How to log out user from web site using BASIC authentication?

后端 未结 22 1424
感情败类
感情败类 2020-11-22 04:00

Is it possible to log out user from a web site if he is using basic authentication?

Killing session is not enough, since, once user is authenticated, each request co

相关标签:
22条回答
  • 2020-11-22 04:52

    type chrome://restart in the address bar and chrome, with all its apps that are running in background, will restart and the Auth password cache will be cleaned.

    0 讨论(0)
  • 2020-11-22 04:53

    Here's a very simple Javascript example using jQuery:

    function logout(to_url) {
        var out = window.location.href.replace(/:\/\//, '://log:out@');
    
        jQuery.get(out).error(function() {
            window.location = to_url;
        });
    }
    

    This log user out without showing him the browser log-in box again, then redirect him to a logged out page

    0 讨论(0)
  • 2020-11-22 04:54
    • use a session ID (cookie)
    • invalidate the session ID on the server
    • Don't accept users with invalid session IDs
    0 讨论(0)
  • 2020-11-22 04:56

    You can do it entirely in JavaScript:

    IE has (for a long time) standard API for clearing Basic Authentication cache:

    document.execCommand("ClearAuthenticationCache")
    

    Should return true when it works. Returns either false, undefined or blows up on other browsers.

    New browsers (as of Dec 2012: Chrome, FireFox, Safari) have "magic" behavior. If they see a successful basic auth request with any bogus other username (let's say logout) they clear the credentials cache and possibly set it for that new bogus user name, which you need to make sure is not a valid user name for viewing content.

    Basic example of that is:

    var p = window.location.protocol + '//'
    // current location must return 200 OK for this GET
    window.location = window.location.href.replace(p, p + 'logout:password@')
    

    An "asynchronous" way of doing the above is to do an AJAX call utilizing the logout username. Example:

    (function(safeLocation){
        var outcome, u, m = "You should be logged out now.";
        // IE has a simple solution for it - API:
        try { outcome = document.execCommand("ClearAuthenticationCache") }catch(e){}
        // Other browsers need a larger solution - AJAX call with special user name - 'logout'.
        if (!outcome) {
            // Let's create an xmlhttp object
            outcome = (function(x){
                if (x) {
                    // the reason we use "random" value for password is 
                    // that browsers cache requests. changing
                    // password effectively behaves like cache-busing.
                    x.open("HEAD", safeLocation || location.href, true, "logout", (new Date()).getTime().toString())
                    x.send("")
                    // x.abort()
                    return 1 // this is **speculative** "We are done." 
                } else {
                    return
                }
            })(window.XMLHttpRequest ? new window.XMLHttpRequest() : ( window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : u ))
        }
        if (!outcome) {
            m = "Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser."
        }
        alert(m)
        // return !!outcome
    })(/*if present URI does not return 200 OK for GET, set some other 200 OK location here*/)
    

    You can make it a bookmarklet too:

    javascript:(function(c){var a,b="You should be logged out now.";try{a=document.execCommand("ClearAuthenticationCache")}catch(d){}a||((a=window.XMLHttpRequest?new window.XMLHttpRequest:window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):void 0)?(a.open("HEAD",c||location.href,!0,"logout",(new Date).getTime().toString()),a.send(""),a=1):a=void 0);a||(b="Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser.");alert(b)})(/*pass safeLocation here if you need*/);

    0 讨论(0)
提交回复
热议问题