Home > Back-end >  Display images via HTTP links on HTTPS page in ASP.NET Core?
Display images via HTTP links on HTTPS page in ASP.NET Core?

Time:10-23

I have a page that shows the favicon.ico in links to several other sites.

My page uses HTTPS, but some of the sites that hose the favicon.ico files are HTTP.

It appears Microsoft Edge, at least, won't display those HTTP resources on an HTTPS page.

It's not practical for me to change all those websites to use HTTPS. And it's kind of important for me to display those icons.

Has anyone found a workaround for this? I know there are questions posted about this, but none of those that offer solutions are using ASP.NET core.

CodePudding user response:

As ProgrammingLlama suggested, I created a proxy to load these icons.

To do this, I created a Razor Pages page handler.

public async Task<IActionResult> OnGetFavIconAsync([FromServices] IHttpClientFactory httpClientFactory, string url)
{
    if (!string.IsNullOrEmpty(url))
    {
        try
        {
            HttpClient client = httpClientFactory.CreateClient();
            HttpResponseMessage response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();
            return File(await response.Content.ReadAsStreamAsync(), "image/x-icon");
        }
        catch (Exception)
        {
        }
    }
    return File("/Images/NoIcon.png", "image/png");
}

And then I referenced the handler from my markup.

<a href="@website.Url">
    <img src="@Url.Page("Index", "FavIcon", new { url = website.IconUrl } )" alt="@website.Title" title="@website.Title"
        style="width: 32px; height: 32px" />
</a>
  • Related