如何在会话超时或结束时注销用户

发布于 2024-10-07 06:04:16 字数 46 浏览 6 评论 0原文

会话结束或过期时注销用户的最佳方法是什么?

感谢您的任何帮助。

Whats the best way to log out a user when a session ends or expires?

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稳稳的幸福 2024-10-14 06:04:16

这实际上取决于您正在寻找的所需功能。我假设您正在使用 FormsAuthentication。

您需要关注两件不同的事情:SessionFormsAuthentication cookie。除非我弄错了,否则这两个都有单独的超时。

如果您遇到的问题是会话超时但用户仍然经过身份验证,您可以尝试以下组合:

1:确保身份验证 cookie 具有与会话相同的超时值:

<authentication mode="Forms"><forms ... timeout="20" ... ><authentication>
<sessionState ... timeout="20" ... />

2:在您的Page_Load事件,检查会话是否超时:(

if (context.Session != null && Context.Session.IsNewSession == true &&
    Page.Request.Headers["Cookie"] != null &&
    Page.Request.Headers["Cookie"].IndexOf("ASP.NET_SessionId") >= 0)
{
    // session has timed out, log out the user
    if (Page.Request.IsAuthenticated)
    {
        FormsAuthentication.SignOut();
    }
    // redirect to timeout page
    Page.Response.Redirect("/Timeout.aspx");
}

参见http://www.eggheadcafe.com/articles/20051228.asp 有关检测会话超时的信息)

如果您想要更愉快的用户体验,您可以使用 javascript 在 X 分钟后启动某种模式 UI 弹出窗口。此弹出窗口仅允许用户启动按钮单击,这将触发服务器上的 AJAX 回发,从而扩展其身份验证和会话 cookie,而无需重新加载页面。我以前从未实现过这个,但是看看, 这家伙做了一个ASP.NET AJAX 控件

It really depends on the desired functionality you're looking for. I'm going to assume you're using FormsAuthentication.

There's two separate things you need to be concerned about: the Session and the FormsAuthentication cookie. Unless I'm mistaken, both of these have separate timeouts.

If the problem you're having is that the session is timed out but the user still is authenticated, you could try a combination of the following:

1: Making sure the authentication cookie has the same timeout value as the session:

<authentication mode="Forms"><forms ... timeout="20" ... ><authentication>
<sessionState ... timeout="20" ... />

2: In your Page_Load event, check if the session has timed out:

if (context.Session != null && Context.Session.IsNewSession == true &&
    Page.Request.Headers["Cookie"] != null &&
    Page.Request.Headers["Cookie"].IndexOf("ASP.NET_SessionId") >= 0)
{
    // session has timed out, log out the user
    if (Page.Request.IsAuthenticated)
    {
        FormsAuthentication.SignOut();
    }
    // redirect to timeout page
    Page.Response.Redirect("/Timeout.aspx");
}

(See http://www.eggheadcafe.com/articles/20051228.asp for information on detecting a session timeout)

If you want a more pleasant user experience, you could use javascript to initiate some sort of a modal UI popup after X minutes. This popup would simply allow a user to initiate a button-click which would trigger an AJAX postback on the server, thus extending their authentication and session cookie without them having to reload the page. I've never implemented this before but look, this guy made an ASP.NET AJAX control !

诗化ㄋ丶相逢 2024-10-14 06:04:16

如果您使用 .net 会员提供程序,只需在 web.config 中设置超时设置
http://msdn.microsoft.com/en-我们/库/h6bb9cz9(v=VS.100).aspx

If you are using the .net Membership Provider, just set the Timeout-setting in the web.config
http://msdn.microsoft.com/en-us/library/h6bb9cz9(v=VS.100).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文