简体   繁体   English

会话在asp.net中处理

[英]session Handling in asp.net

I am trying to find out how to destroy a ASP.NET session when the user navigates away from my site.I am using Global.ascx application file and use session_end event .Its working fine when i click logout button. 我试图找出当用户导航离开我的网站时如何销毁ASP.NET会话。我正在使用Global.ascx应用程序文件并使用session_end事件。当我点击注销按钮时工作正常。 But when i close browser directly its not using Global file after 20 minitues. 但是当我直接关闭浏览器时,它会在20分钟之后不使用Global文件。 i used in web.conig file as <sessionState mode="InProc" timeout="20"></sessionState> 我在web.conig文件中用作<sessionState mode="InProc" timeout="20"></sessionState>
Please help me 请帮我
Augustine 奥古斯丁

Session timeout defines when some session would be destroyed after some idle time. 会话超时定义了在某个空闲时间之后某个会话将被销毁的时间。

If you want more control over sessions, you would need to use SQLServer mode, State Server mode, or a custom one taylored yourself. 如果您想要更多地控制会话,则需要使用SQLServer模式,State Server模式或自己定制的自定义模式。

I mean that, because, for example, SQLServer mode stores sessions in a standard SQL Server table, so, you can implement some SQL Server job, or schedule some task in Windows or in some Windows Service, so you can clean up sessions depending on your needs. 我的意思是,因为,例如,SQLServer模式将会话存储在标准SQL Server表中,因此,您可以实现某些SQL Server作业,或在Windows或某些Windows服务中安排某些任务,这样您就可以根据您的需求。

By the way, maybe you need to know that if you're using InProc, your sessions can be destroyed because IIS recycled application pool, or server got entirely restarted. 顺便说一句,也许您需要知道,如果您使用InProc,您的会话可能会被破坏,因为IIS回收的应用程序池或服务器完全重新启动。 And this isn't ending sessions, so the appropiate event and handler won't work in this scenario. 这不是结束会话,因此适当的事件和处理程序在这种情况下不起作用。

I repeat, if you need a better session management mode like ones I suggested you. 我再说一遍,如果你需要一个更好的会话管理模式,就像我建议的那样。


EDIT: 编辑:

Check this page: http://aspalliance.com/520_Detecting_ASPNET_Session_Timeouts.2 查看此页面: http//aspalliance.com/520_Detecting_ASPNET_Session_Timeouts.2

You'll learn how to handle session timeouts better, since Session_End isn't called when a session expires, but when you call "Session.Abandon". 您将学习如何更好地处理会话超时,因为Session_End在会话到期时未被调用,但是当您调用“Session.Abandon”时。

The browser doesn't send a signal to the website when the visitor "navigates away" or "closes his browser". 当访问者“导航”或“关闭他的浏览器”时,浏览器不会向网站发送信号。 So asp.net (or any other server side technology) can't react to that to destroy a session. 所以asp.net(或任何其他服务器端技术)无法对此做出反应来销毁会话。

That's why a session works with a timeout: if the user doesn't perform a request within the timeout period, then he is supposed to have abandoned the session, even when he was just reading that page for 20+ minutes. 这就是为什么会话在超时期间工作的原因:如果用户在超时期限内没有执行请求,那么他应该放弃会话,即使他刚刚读取该页面超过20分钟。

您可以使用inProc模式的SQLServer模式instread

Create a New page that calls session.End on Page Load and Closes the Page (Window.Close()) using RegisterClientScriptBlock. 创建一个新页面,使用RegisterClientScriptBlock在页面加载和关闭页面(Window.Close())上调用session.End。 Call this new Page window.Location on unload of the current page using Javascript. 调用这个新的Page window.Location使用Javascript卸载当前页面。

你可以通过调用Session.Abandon()来杀死Session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM