I want users to be able to log in via HTTP Basic authentication modes.
The problem is that I also want them to be able to log out again - weirdly browsers just don't seem to support that.
This is considered to be a social-hacking risk - user leaves their machine unlocked and their browser open and someone else can easily visit the site as them. Note that just closing the browser-tab is not enough to reset the token, so it could be an easy thing for users to miss.
So I've come up with a workaround, but it's a total cludge:
1) Redirect them to a Logoff page
2) On that page fire a script to ajax load another page with dummy credentials:
$j.ajax({ url: '<%:Url.Action("LogOff401", new { id = random })%>', type: 'POST', username: '<%:random%>', password: '<%:random%>', success: function () { alert('logged off'); } });
3) That should always return 401 the first time (to force the new credentials to be passed) and then only accept the dummy credentials:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult LogOff401(string id) { // if we've been passed HTTP authorisation string httpAuth = this.Request.Headers["Authorization"]; if (!string.IsNullOrEmpty(httpAuth) && httpAuth.StartsWith("basic", StringComparison.OrdinalIgnoreCase)) { // build the string we expect - don't allow regular users to pass byte[] enc = Encoding.UTF8.GetBytes(id + ':' + id); string expected = "basic " + Convert.ToBase64String(enc); if (string.Equals(httpAuth, expected, StringComparison.OrdinalIgnoreCase)) { return Content("You are logged out."); } } // return a request for an HTTP basic auth token, this will cause XmlHttp to pass the new header this.Response.StatusCode = 401; this.Response.StatusDescription = "Unauthorized"; this.Response.AppendHeader("WWW-Authenticate", "basic realm=\"My Realm\""); return Content("Force AJAX component to sent header"); }
4) Now the random string credentials have been accepted and cached by the browser instead. When they visit another page it will try to use them, fail, and then prompt for the right ones.
Note that my code examples are using jQuery and ASP.Net MVC, but the same thing should be possible with any technology stack.
There's another way to do this in IE6 and above:
document.execCommand("ClearAuthenticationCache");
However that clears all authentication - they log out of my site and they're logged out of their e-mail too. So that's out.
Is there any better way to do this?
I've seen other questions on this, but they're 2 years old - is there any better way now in IE9, FX4, Chrome etc?
If there is no better way to do this can this cludge be relied upon? Is there any way to make it more robust?
We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.
1 Answer. It is indeed not possible to pass the username and password via query parameters in standard HTTP auth. Instead, you use a special URL format, like this: http://username:[email protected]/ -- this sends the credentials in the standard HTTP "Authorization" header.
HTTP basic authentication is a simple challenge and response mechanism with which a server can request authentication information (a user ID and password) from a client. The client passes the authentication information to the server in an Authorization header. The authentication information is in base-64 encoding.
Because Basic authentication involves the cleartext transmission of passwords, it SHOULD NOT be used (without enhancements such as HTTPS RFC2818) to protect sensitive or valuable information. As the user ID and password are passed over the network as clear text ... the basic authentication scheme is not secure.
The short anser is:
There is no reliable procedure for achieving a "logoff" using HTTP Basic or Digest authentication given current implemenations of basic auth.
Such authentication works by having the client add an Authorization header to the request.
If for a certain resource the server is not satisfied with the credentials provided (e.g. if there are none), it will responde with a "401 Unauthorized" status code and request authentication. For that purpose it will provide a WWW-Authenticate header with the response.
A client need not wait for a server requesting authentication. It may simply provide an Authorization header based on some local assumptions (e.g. cached information from the last successful attempt).
While your outlined approach on "clearing" out authentication info has a good chance of working with a wide range of clients (namely widespread browsers), there is absolutely no guarantee that a nother client might be "smarter" and simply discriminate proper authentication data for your "logout" page and any other pages of the target site.
You will recognize a similar "problem" with using client side certificate based authentication. As long as there is no explicit support from clients you might fight on lost ground.
So, if "logoff" is a concern, move over to any session based authentication.
If you have access to the implementation of authentication on the server side you might be able implementing a functionality that will disregard authentication information presented with Authorization header (if still identical to what has been presented during current "session) on request of your application level code (or provide some "timout" after which any credentials will be re-requested), so that the client will ask the user for providing "new" credentials (performing a new login).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With