简体   繁体   English

如何在按下标题栏关闭按钮asp.net c#时捕获会话结束

[英]how to catch session end when press title bar close button asp.net c#

I have some question about asp.net web application. 我对asp.net Web应用程序有疑问。 Web application which use sql server session state. 使用sql server会话状态的Web应用程序。 Logout event can session remove but press title bar close button want to remove session. 注销事件可以删除会话,但是按标题栏关闭按钮想要删除会话。 Which events can control when press title bar close button. 按下标题栏关闭按钮可以控制哪些事件。 I read about session_end event can catch when press title bar close button. 我读到有关session_end事件可以在按标题栏关闭按钮时捕获的信息。 But can't arise session_end event using sql server session state. 但是无法使用SQL Server会话状态发生session_end事件。 How can to catch? 如何捕捉?

Closing the browser does not end the session on the server, the server can't tell the difference between an open or closed browser, it simply receives a request and then sends a response and sits there waiting for another request. 关闭浏览器并不会结束服务器上的会话,服务器无法分辨打开的浏览器还是关闭的浏览器,它只是接收一个请求,然后发送响应,然后坐在那里等待另一个请求。 After a certain amount of time with no requests the session will time out and the session_end event will run. 在没有请求的一定时间后,会话将超时并且session_end事件将运行。

If you wanted to end the session would have to have some kind of ajax request post back to the server when you closed the browser in order for the server to know to end the session. 如果要结束会话,则在关闭浏览器时必须将某种Ajax请求发回到服务器,以便服务器知道结束会话。

EDIT 编辑

you could do something like this using jQuery 您可以使用jQuery做类似的事情

$(window).unload(function() {
  $.post('someurl');
});

replace someurl with the url of a web service, handler or mvc controller action or similar that tells the server that the browser has been closed. 将web服务,处理程序或mvc控制器操作或类似的URL替换为URL,以告知服务器浏览器已关闭。

Then server side you just do 然后服务器端就可以了

Session.Abandon();

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

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