views:

20

answers:

2

I would like to display the image from a network camera on my web page, but the image is behind a HTTP basic authentication server.

In Firefox and Chrome I can do this:

<img width="320" height="200" src="http://username:password@server/Path" />

But in Internet Explorer 8, I get an empty image box. If I use JQuery to set the src attribute, IE8 displays a blank src. It looks like IE8 is checking the string and rejecting it.

Is there a way to put the basic authentication credentials in the img tag?

A: 

You can load your img with AJAX, using XMLHttpRequest. As you might know, XMLHttpRequest has a setRequestHeaders method, so you will be able to manipulate headers for your request, hence, you will be able to do basic HTTP authentication.

Pablo Santa Cruz
You can pass auth info in parameters to `open()`. Unfortunately extracting the binary content of a response, and injecting it into an image node on the page, isn't so easy.
bobince
A: 

Bottom line: Not all browsers allow this. It may work in some but not others.

But as someone else has said already, it's not very safe -- you're effectively giving the login and password details to anyone who browses the page. Not good.

A better option would be proxy it through the same server that you're providing the html code from, then the href in the <img> tag could just be a local URL, and no-one need know where the image is actually coming from.

Spudley
"Not all browsers allow this. It may work in some but not others." Can you cite your source?
Robert
@Robert - no source; just experience; I had a similar frustration to you some time ago, trying something very similar. :) But that said, I may be out of date as I haven't actually tried doing this sort of thing for quite a while, due to my other point; ie it's not very safe.
Spudley
also, to clarify: there's a difference in how browsers will treat it if you enter `name:password@server/path` in the address bar, vs if it's in a link. Because passwords are involved, the browser's security policy comes into play.
Spudley