Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the <img> tag with basic authentication

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?

like image 302
Robert Avatar asked Sep 29 '10 15:09

Robert


People also ask

Are the basic attributes of IMG tag?

The img element has two required attributes: src : The source location (URL) of the image file. alt : The alternate text. This is used to describe the image for someone who cannot see it because they are either using a screen reader or the image src is missing.

How do I make basic authentication secure?

Basic Auth creds are "plaintext over SSL" just like sending your credit card number through HTTP POST is "plaintext over SSL". So by doing it over SSL (TLS these days) makes it secure during transport.


2 Answers

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.

like image 196
Spudley Avatar answered Oct 14 '22 08:10

Spudley


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.

like image 26
Pablo Santa Cruz Avatar answered Oct 14 '22 09:10

Pablo Santa Cruz