November 9, 2012

a href link with basic authentication

Question by Anna Smother

I have a website were I login via basic authentication, that works fine. But when I want to download a .pdf file (that is generated by the server) I get a popup with the information that I need to login…again :/.

How can I prevent this? Or how can I send the basic authentication information with the link? I saw some examples, like: http://username:password@yourserver.com/filename.pdf . But that don’t work in IE.

How can I solve this with only html and or jQuery?

edit:
I login via a form in the site, that works. But when I want to download an .pdf file while i’m logged in, I get the pop-up.

<a href="http://yourserver.com/yourpdffile.pdf">Report1.pdf</a>

Answer by Starx

Basic authentication is Server based, HTML and jQuery are helpless here.

However, before sending AJAX request to such page, you can provide these details. Here is an example.

$.ajax({
    'url': 'http://yourdomain.com/action/',
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) 
    },
    //.....
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

a href link with basic authentication

Question by Anna Smother

I have a website were I login via basic authentication, that works fine. But when I want to download a .pdf file (that is generated by the server) I get a popup with the information that I need to login…again :/.

How can I prevent this? Or how can I send the basic authentication information with the link? I saw some examples, like: http://username:password@yourserver.com/filename.pdf . But that don’t work in IE.

How can I solve this with only html and or jQuery?

edit:
I login via a form in the site, that works. But when I want to download an .pdf file while i’m logged in, I get the pop-up.

<a href="http://yourserver.com/yourpdffile.pdf">Report1.pdf</a>

Answer by Starx

Basic authentication is Server based, HTML and jQuery are helpless here.

However, before sending AJAX request to such page, you can provide these details. Here is an example.

$.ajax({
    'url': 'http://yourdomain.com/action/',
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) 
    },
    //.....
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!