分享

HTTP Basic Authentication验证方式的注销(logout)方法(转)

 _小女子_ 2013-08-26
Basic Authentication方式的注销是个难题,在网上寻找找到了一段js代码,我稍加修改了下,可以很方便地注销掉这种登录。


function clearAuthenticationCache(page) {
  if (!page) page = '/';
  try {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("msie") != -1) {
      // IE clear HTTP Authentication
      document.execCommand("ClearAuthenticationCache");
    }
    else if (agt.indexOf("firefox") != -1) {
      // Let's create an xmlhttp object
      var xmlhttp = createXMLObject();
      // Let's prepare invalid credentials
      xmlhttp.open("GET", page, true, "logout", "logout");
      // Let's send the request to the server
      xmlhttp.send("");
      // Let's abort the request
      xmlhttp.abort();
    }
    else {
      window.close();
    }
  } catch(e) {
    // There was an error
    return;
  }
}
    
function createXMLObject() {
  try {
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  } catch (e) {
    xmlhttp=false
  }
  return xmlhttp;
}

其中IE浏览器是通过调用内部命令ClearAuthenticationCache实现的;Firefox是通过Ajax发送一个错误的账号密码 (logout:logout)实现,所以系统中就不要有叫logout密码也为logout的用户;其它浏览器就直接关闭页面,但如Opera事实上是 没有注销的。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多