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

后端 未结 22 1938
感情败类
感情败类 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:42

    The following function is confirmed working for Firefox 40, Chrome 44, Opera 31 and IE 11.
    Bowser is used for browser detection, jQuery is also used.

    - secUrl is the url to a password protected area from which to log out.
    - redirUrl is the url to a non password protected area (logout success page).
    - you might wish to increase the redirect timer (currently 200ms).

    function logout(secUrl, redirUrl) {
        if (bowser.msie) {
            document.execCommand('ClearAuthenticationCache', 'false');
        } else if (bowser.gecko) {
            $.ajax({
                async: false,
                url: secUrl,
                type: 'GET',
                username: 'logout'
            });
        } else if (bowser.webkit) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", secUrl, true);
            xmlhttp.setRequestHeader("Authorization", "Basic logout");
            xmlhttp.send();
        } else {
            alert("Logging out automatically is unsupported for " + bowser.name
                + "\nYou must close the browser to log out.");
        }
        setTimeout(function () {
            window.location.href = redirUrl;
        }, 200);
    }

提交回复
热议问题