session Handling in asp.net

后端 未结 5 788
北荒
北荒 2020-12-09 21:01

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 wor

相关标签:
5条回答
  • 2020-12-09 21:42

    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.

    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.

    0 讨论(0)
  • 2020-12-09 21:43

    you can kill the Session by calling the Session.Abandon()

    0 讨论(0)
  • 2020-12-09 21:44

    Create a New page that calls session.End on Page Load and Closes the Page (Window.Close()) using RegisterClientScriptBlock. Call this new Page window.Location on unload of the current page using Javascript.

    0 讨论(0)
  • 2020-12-09 21:51

    You can use SQLServer mode instread of inProc mode

    0 讨论(0)
  • 2020-12-09 22:00

    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.

    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.

    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. 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

    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".

    0 讨论(0)
提交回复
热议问题