Search code examples
c#asp.net-mvc-4mixed-content

Images from http site on https site: mixed mode


My https-based site (site A) uses images from http-based site B. I causes mixed-content error. To fix this, I found solution to swap each external link like http://www.siteB.com/imageX.png with my controller method which do forward to external image. The new link format is:

The code of method /api/misc/forward is following:

 [HttpGet]
    public async Task<HttpResponseMessage> Forward(string url)
    {
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();

        try
        {

            var response = Request.CreateResponse(HttpStatusCode.Found);
            response.Headers.Location = new Uri(HttpUtility.UrlDecode(url));
            return response;
        }
        catch (Exception ex)
        {
            httpResponseMessage.StatusCode = HttpStatusCode.NotFound;
            _loggerService.LogException(ex, url);
        }

        return httpResponseMessage;
    }

but the browser still is able to recognize it as mixed mode.... Why? The original image links sent to browser origins from https-based site.

Any quick tip for it? I dont want to cache all images from site B:).


Solution

  • Because your code sends back a redirect to another location, so, eventually, the browser still go to the HTTP image.

    What happens is that your browser calls the controller in HTTPS, then controller action sends back a redirect command to the browser, the browser retrieves the image from the new location that you set in the response.Headers.Location.

    If you want to avoid the mixed mode, then you need to retrieve the image from the controller and return a FileResult from the action, this way, the browser will not have to access the HTTP site.

    Another approach, would be to just copy the images to you site.