【发布时间】:2012-06-22 21:05:22
【问题描述】:
当浏览器选项卡关闭时,我需要在 ASP.NET 中可靠地注销经过身份验证的用户。推荐的解决方案是什么?
谢谢
【问题讨论】:
-
(重复)看看这个答案,它涵盖了同样的问题:stackoverflow.com/a/287027/328968
标签: asp.net forms authentication forms-authentication
当浏览器选项卡关闭时,我需要在 ASP.NET 中可靠地注销经过身份验证的用户。推荐的解决方案是什么?
谢谢
【问题讨论】:
标签: asp.net forms authentication forms-authentication
通常您会在会话结束时执行退出逻辑。但是,如果您必须检测页面何时关闭,请使用:
<body onunload="performMyLogoutLogic();">
...
...
</body>
【讨论】:
您可以使用通用处理程序来终止会话并在卸载之前调用它,如下所示:
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!");
}
}
}
【讨论】: