0

On my WPF Window I have an Image whose UriSource is set to the URL of some image on the internet.

<Image>
    <Image.Source>
        <BitmapImage UriSource="http://tinyurl.com/anmucph" />
    </Image.Source>
</Image>

Everything works properly when I use a publicly accessible image on the internet: the WPF framework does the necessary HTTP GET request and displays the image.

What I want to do is use a UriSource that is a URL that requires the HTTP GET request to come with an Authorization header. (I have the required Authorization header string that I want to use.)

How do I use a UriSource that requires authorization? Is there something built into WPF? Or does this require a custom solution? If so, how can I hook into the WPF framework to provide custom logic for performing the HTTP GET?

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173

3 Answers3

3

i know this is an old thread but i wanted to follow up with the solution i found.

.net has a cool feature to let you override it's core with your own methods on a system or application level. this can be done for the WebRequest.Create method

see http://msdn.microsoft.com/en-us/library/bc0fhzk5.aspx

this post is a full example including IWebRequestCreate so you don't get stuck in recursion.

Add header to all* HTTP requests on a machine

after all this, everything in your application scope will be forced to use your create method.

NOTE: before adding your auth header, make sure you are on the right domain so you don't give out your auth to other places :)

Community
  • 1
  • 1
Adam Duffy
  • 31
  • 2
0

Do you have the username and password for the resource? You can specify the domain name like this if it's just standard HTTP auth:

http://username:password@tinyurl.com/anmucph
edcs
  • 3,847
  • 2
  • 33
  • 56
  • 1
    If I do this from a browser - I tried Chrome and Firefox - it will add on the appropriate "Basic Base64({username}:{password})" Authorization header. But this doesn't seem to work in WPF. – Timothy Shields Feb 19 '13 at 22:42
  • This is [not supported in IE](https://support.microsoft.com/kb/834489/en-us?wa=wsignin1.0) and hence WPF won't work either! – akshay2000 Jan 05 '15 at 16:54
0

This can't be done with the tools built into WPF.

An alternative would be to create a lightweight and local HTTP proxy that has a single

  • GET http://localhost:####/proxy?uri={uri}&authorization={authorization}

operation (where port #### is behind firewall) that in turn makes a

  • GET {uri}

request with the supplied authorization header value.

You could then set UriSource to http://localhost:####/proxy?uri=X&authorization=Y where X is the URI-encoded URI of the secured resource you want to show and Y is the URI-encoded authorization header to send with the GET request.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173