像 stackoverflow 一样使会话永远保持活动状态
我需要保持会话处于活动状态,除非用户在我的 asp.net mvc(C#) 应用程序中单击注销。
当用户关闭浏览器并再次打开时,会话应继续使用这些值。
我正在尝试像 stackoverflow 中那样实现。
有什么想法/建议吗?
I need to keep the session live unless until the user clicks logout in my asp.net mvc(C#) application.
When the user closes the browser and opens again, the session should continue with the values.
I am trying to implement as in stackoverflow.
Any ideas/suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您说您希望“像在 StackOverflow 中一样”保持会话处于活动状态。...StackOverflow 与大多数安全站点一样,不会无限期地保持会话处于活动状态。它使用 cookie 来“记住”登录信息。
You say you want to keep the session alive "as in StackOverflow."... StackOverflow, like most secure sites, does not keep sessions alive indefinitely. It uses cookies to "remember" the login.
如果您使用 FormsAuthentication,您可以执行以下操作:
这将创建一个在不同浏览器会话中保留的 cookie,并实现您正在寻找的内容。
if you use FormsAuthentication, you can do something like:
That will create a cookie that is persisted across different browser sessions, and will achieve what you're looking for.
如果您想记住“状态”,即使(由于会话/会话 cookie 过期)您强制用户再次登录。您需要保留会话数据。也许您的网络容器可以为您做到这一点。
If you want to remember 'state' even when (because of the expired session / session cookie) you are forcing your users to login again. You need to persist the session data. Perhaps your web-container can do this for you.
首先,如果您想要创建多会话但临时数据,您可能应该查看 ASP.NET 用户配置文件。
如果您想跨会话保持登录,请查看 FormsAuthentication 中用于记住用户的部分。
如果您需要无限期地保持会话活动而不永远设置超时(因此在某些情况下会触发服务器管理员的谋杀),一个巧妙的技巧是设置一个 Ajax“心跳”,以便在浏览器打开且有效时 ping 回服务器做一个“让这个会话保持活力”的技巧。
First, if you want to make multi-session but temp data, you should probably look into the ASP.NET user profile.
If you want to persist logins across sessions, look at the bits of FormsAuthentication that deal with remembering the user.
If you need to keep sessions alive indefinitely without setting the timeout forever (therefor triggering murder by the server admin in some cases), a neat trick is to setup an Ajax "heartbeat" to ping back to the server while the browser is open and effectively do a "keep this session alive" trick.
当浏览器关闭时会话将会丢失
The session will be lose when the browser is closed