简体   繁体   English

为什么“Session_end”不起作用?

[英]Why “Session_end” not working?

The event in the global.asax file is written like this: global.asax文件中的事件如下所示:

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. 默认会话到期超时为20分钟,因此在客户端关闭浏览器20分钟后将调用此事件。

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. 哦,你应该注意一个非常重要的问题:如果你使用的是进程外会话状态模式(例如StateServer或SqlServer),那么永远不会调用此事件。

PS: The Session_End event is only suported by the InProc session manager PS: Session_End事件仅由InProc会话管理器支持

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). 我认为你的需要是在页面关闭或文档解密时使用javascript函数,即onbeforeunload 。并在该函数中向服务器发出ajax请求,指示客户端正在关闭窗口。但是你应该注意安全性,因为这个javascript在客户端注册的方法,恶意用户可以更改ajax请求(甚至阻止它)。 If you dont consider any security issue, you are good to go.Good luck. 如果你不考虑任何安全问题,你很高兴。好运。

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

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