Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle www-authentication request using ajax?

Tags:

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?

like image 219
mikkelbreum Avatar asked Apr 27 '09 10:04

mikkelbreum


People also ask

Can AJAX work with Web application?

Ajax stands for Asynchronous JavaScript and XML. In essence, Ajax is an efficient way for a web application to handle user interactions with a web page - a way that reduces the need to do a page refresh or full page reload for every user interaction.

Does AJAX work with HTML?

AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)

Are AJAX requests HTTP requests?

Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method. To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET).


1 Answers

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"></script>
<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>
like image 141
James Hall Avatar answered Oct 11 '22 21:10

James Hall