如何获得通知会话结束的? [英] How to get notified of session end?

查看:171
本文介绍了如何获得通知会话结束的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我能处理的会话超时事件。我需要做一个函数调用我的功能权限之前会话超时或用户离开我的网页,或关闭浏览器窗口。这里最重要的部分是可以访问会话期间存储的一切,会话变量。

I am wondering if I can handle a session timeout event. I need to make a function call to my function right before session timeouts or user left my page, or closed browser window. The most important part here is to have access to everything stored during the session, session variables.

推荐答案

看一看在Global.asax中的Session_End中()方法。 Global.asax文件应该已经连接会话结束时调用此方法。你可以把你的code在那里,但是你会喜欢的。

Take a look at the Session_End() method in global.asax. The global.asax file should already be wired to call this method when the session ends. You can just put your code in there however you'd like.

	Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
		' Fires when the session ends
	End Sub

当然,你应该注意到,本次会议完全是一个服务器端的事情。它只是将闲置的X分钟后结束(20分钟是默认值)。它不会结束用户关闭窗口的时刻。 ASP.NET没有用户的知识关闭窗口或者离开你的页面。

Of course, you should note that the session is entirely a server-side thing. It'll only end after X minutes of inactivity (20 minutes is the default). It will not end the instant the user closes the window. ASP.NET has no knowledge of a user closing a window or leaving your page.

而事实上,这是棘手的尝试在服务器端执行任何立即采取行动,当这种情况发生。我的意思是,它很容易调用window.onbeforeunload在Javascript和弹出确认框的用户关闭窗口前。但是,当你尝试将数据包发送到窗口实际上关闭之前立即服务器,我想你会发现,它成为一个艰巨的任务,使这一切方便用户使用。你可以写一些JavaScript将消息发送到使用AJAX其通知用户关闭浏览器的服务器的服务器,然后用更多的JavaScript关闭浏览器已经发送了消息之后。但随后用户将得到恼人的消息(在大多数浏览器),指出某个脚本试图关闭窗口。这会显得很业余十岁上下给用户,可能会让他们三思回来了。

And in fact, it's tricky to try to perform any immediate action on the server-side when this happens. I mean, it's easy to call window.onbeforeunload in Javascript and bring up a confirm box before the user closes the window. But when you try to send a packet to the server immediately before the window actually closes, I think you'll find that it becomes a difficult task to make this all user-friendly. You could write some Javascript that sends a message to the server using AJAX which notifies the server that the user is closing their browser, and then use more Javascript to close the browser after that message has been sent. But then the user will get the annoying message (in most browsers) stating that a script is trying to close the window. This will look very amateur-ish to the user and may make them think twice about coming back.

所以,如果你愿意忍受的延迟,我只希望避免这整个混乱的Javascript完全使用Session_End中依靠一个纯粹的服务器端解决方案()。毕竟,如果用户关闭窗口或树叶,ASP.NET会的最后的通知,因为会议将超时。

So if you're willing to put up with a delay, I'd just avoid this whole Javascript mess entirely and rely on a pure server-side solution using Session_End(). After all, if a user closes the window or leaves, ASP.NET will eventually notice because the session will timeout.

这篇关于如何获得通知会话结束的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆