开发者

Logging a user out when using HTTP Basic authentication

开发者 https://www.devze.com 2023-03-11 16:30 出处:网络
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 - 开发者_StackOverflowweirdly browsers just don\'t seem to supp

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 - 开发者_StackOverflowweirdly 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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号