How to set the img tag with basic authentication
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.
Authorization header in img src link
You can not perform authentication on images which are directly used as href in img tag. If you really want this type of authentication on your images, then it's better to fetch them using ajax and then embed in your html.
How to show an image which is secured by http authentication
I used IrishGeeks tip to get a solution. It works on all browsers. The script.php is
<?php
$url = $_GET['url'];
$c = curl_init($url);
$authString = 'user:pass';
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, $authString);
$content = curl_exec($c);
$contentType = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
header('Content-Type:'.$contentType);
print $content;
?>
Then use
<?php
print '<img src="script.php?url='.urlencode('http://www.example.com/image.bmp').'" />';
?>
To get the image.
Display an image in img-Tag from a basic-authed directory
This was easily made by creating a new get_images.php file which will open the Image using fopen() and fpasssthru()
then in the tag you can set src="get_image.php"
How to get src from URL which requires basic auth
Well , it seems the the proper way to implement such behaviour is the following:
<audio id= "audioElement" controls="controls"> Your browser does not support the HTML5 Audio element. </audio>
<script>
window.onload = function()
{
var oReq = new XMLHttpRequest();
oReq.open('GET', 'https://upload.wikimedia.org/wikipedia/ru/6/62/Meow.ogg', true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent)
{
var arrayBuffer = oReq.response;
if (arrayBuffer)
{
var u8 = new Uint8Array(arrayBuffer);
var b64encoded = btoa(String.fromCharCode.apply(null, u8));
var mimetype="audio/ogg"; // or whatever mime type is
document.getElementById("audioElement").src="data:"+mimetype+";base64,"+b64encoded;
}
}
oReq.send(null);
}
</script>
Hopefully, it will work with basic auth too...
Setting credentials for img basic authentication
I ended up using the WebClient class to set the credentials to authenticate to the other server and then download the photo locally:
WebClient myWC = new WebClient();
myWC.Credentials = new System.Net.NetworkCredential(bioSearchUsername, bioSearchPassword);
string photoPath = Server.MapPath("~/images/Employees/" + employee + ".jpg");
myWC.DownloadFile(imageLocation, photoPath);
After that, I referenced the local copy of the photo on the server:
imgPhoto.ImageUrl = photoPath;
This got around the authentication problems I was running into with the image.
Related Topics
Remove .HTML and .PHP Extensions with .Htaccess
How to Float a Div Over Google Maps
Convert Embedded Svg to Png In-Place
How to Print a Portion of an HTML Page
CSS Overflow Hidden with Absolute Position
How to Change a Span to Look Like a Pre with CSS
Auto Resize Image in CSS Flexbox Layout and Keeping Aspect Ratio
Transparent Scrollbar with CSS
Regex to Match All HTML Tags Except <P> and </P>
Xpath to Select Between Two HTML Comments Is Not Working
Bad Value X-Ua-Compatible for Attribute Http-Equiv on Element Meta
How to Communicate Between Frames
Paragraph of Text in Circle Using CSS
How to Specify Parameters to Google Chrome Adobe PDF Viewer
Equivalent to Float in Outlook