简体   繁体   中英

Why “Session_end” not working?

The event in the global.asax file is written like this:

protected void Session_End(Object sender, EventArgs e)
{
    var x = 5;
}

The code does not reach there when the client close the window, why?

The code does not reach there when the client close the window, why?

Because this event is not supposed to be invoked when the client closes his browser. This event (as its name suggests) is intended to be invoked when the session expires. The default session expiration timeout is 20 minutes so this event will be invoked 20 minutes after the client has closed his browser.

Oh and there's a very important gotcha you should be aware of: if you are using an out-of-process session state mode (such as StateServer or SqlServer) this event will never be invoked.

PS: The Session_End event is only suported by the InProc session manager

It gets called when your session get expired.

I think your need is use a javascript function when page is closing or document unloding, that is onbeforeunload .And in that function make an ajax request to server indicating that client is closing the window.But you should be careful about security, because this javascript method registered at client side, malicious users can change the ajax request(even prevent it). If you dont consider any security issue, you are good to go.Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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