【问题标题】:ASP.NET - Sign out user on browser tab closedASP.NET - 关闭浏览器选项卡上的注销用户
【发布时间】:2012-06-22 21:05:22
【问题描述】:

当浏览器选项卡关闭时,我需要在 ASP.NET 中可靠地注销经过身份验证的用户。推荐的解决方案是什么?

谢谢

【问题讨论】:

标签: asp.net forms authentication forms-authentication


【解决方案1】:

通常您会在会话结束时执行退出逻辑。但是,如果您必须检测页面何时关闭,请使用:

<body onunload="performMyLogoutLogic();">
...
...
</body>

【讨论】:

  • 如果用户只是从页面导航到同一站点上的不同页面,这也会将用户注销。
【解决方案2】:

您可以使用通用处理程序来终止会话并在卸载之前调用它,如下所示:

function CloseSession( )
{
    location.href = 'KillSession.ashx?task=1'; 
}
window.onbeforeunload = CloseSession;

在你的 KillSession.ashx 中这样做

 public void ProcessRequest(HttpContext context)
            {
                if(!String.IsNullOrWhiteSpace(Request.QueryString["task"].toString()))
                {
                 if(Request.QueryString["task"].toString()=="1")
                  {
                    Session["User"]==null;
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Good Bye!");
                   }
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2014-10-12
    • 1970-01-01
    • 2014-07-01
    • 2012-08-19
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多