views:

482

answers:

2

Is it possible to pass credentials using AJAX to a webserver that request www-authentication?

I want to log in to a website that uses .NET Bsic www-authentication, and pass the credentials using ajax. When visiting the server with a browser, the browser prompts the user with an authentication/login window.

The html header contains this:

WWW-Authenticate: Basic
realm="hosting.xp"
MicrosoftSharePointTeamServices: 6.0.2.6568
X-Powered-By: ASP.NET

I want to access the site 'behind the scenes' by calling it from an ajax object, but I'm not sure how to handle the http header that requests the authentication.

I would like the ajax call to result in a specific user being logged in (the cookie set) so that the user can procedd to the site later and be 'already' logged in.

can this be done in the way I describe here?

A: 

With http-authentication there is no need for cookies, the credentials are kept by the browser, or in your case for the initial request, by your javascript state.

+3  A: 

Hey,

You can pass the username and password in the URL like so:

http://username:[email protected]/secure

Here's an example with jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(document).ready(function(){
    $('a.logMeIn').click(function(){
     $.get('http://username:[email protected]/secure', null, function(response) {
      alert(response);
     });
    });
});

</script>

<a href="#" class="logMeIn">Log me in!</a>
James Hall
This would pose a problem if the password has something other than alphanumeric characters in it...how would you work around that?
sunmorgus