4

I've encountered a problem with loading URL images in WPF .Net Core 3.0. I think it is a problem related to the .Net version rather than a code-related issue or anything (given that I don't have the issue in .Net Framework 4.7.2).

This is the problematic line: <Image Source="http://cdn-www.airliners.net/aviation-photos/photos/7/0/1/1892107.jpg"/>

I need to have the image in the URL format, I am pulling it in from an SQL database. By setting the source directly to URL in the example, I believe I've eliminated any possible server-related issues (the original app is a little bit more complicated, but not even this simplified one-liner works).

0

1 Answer 1

3

The problem is due to the URL having a series of redirects that includes going from HTTPS to HTTP, which is not secure.

You can see the redirects that are performed by loading the original URL in a browser and checking the network request tab in the developer console:

so many redirects

You can confirm that this is causing an error by adding a handler to the Image control's ImageFailed event:

Image control:

<Image Source="http://cdn-www.airliners.net/aviation-photos/photos/7/0/1/1892107.jpg" 
        ImageFailed="Image_ImageFailed" />

Event handler:

private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
    Debug.WriteLine(e.ErrorException.Message);
}

This will result in the following error message being printed to the debug output:

The remote server returned an error: (301) Moved Permanently.

Why does this only happen in .NET Core?

Image uses HttpWebRequest underneath to resolve URL sources. With .NET Core, the HttpWebRequest behavior was intentionally changed to not allow following HTTPS -> HTTP redirects because it is a security risk. You can check out this issue for details, especially this post from David Shulman:

Yes, this is by-design in .NET Core. This is a security-related change to prevent HTTPS -> HTTP redirection which is inherently insecure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.