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

编程入门 行业动态 更新时间:2024-10-25 06:32:20
本文介绍了如何在会话超时或结束时注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

感谢您的帮助.

推荐答案

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

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

您需要关注两件独立的事情:Session 和 FormsAuthentication cookie.除非我弄错了,否则两者都有不同的超时时间.

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:确保身份验证 cookie 与会话具有相同的超时值:

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:在您的 Page_Load 事件中,检查会话是否超时:

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"); }

(有关检测的信息,请参见 www.eggheadcafe/articles/20051228.asp会话超时)

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

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

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 !

更多推荐

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

本文发布于:2023-11-12 01:46:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结束时   用户   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!