如何获得会话结束的通知?

发布于 2024-08-05 02:28:39 字数 92 浏览 3 评论 0原文

我想知道是否可以处理会话超时事件。我需要在会话超时或用户离开我的页面或关闭浏览器窗口之前对我的函数进行函数调用。这里最重要的部分是访问会话期间存储的所有内容,即会话变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聚集的泪 2024-08-12 02:28:39

看一下 global.asax 中的 Session_End() 方法。 global.asax 文件应该已连接以在会话结束时调用此方法。您可以根据需要将代码放入其中。

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

当然,您应该注意会话完全是服务器端的事情。它只会在 X 分钟不活动后结束(默认为 20 分钟)。它不会在用户关闭窗口时立即结束。 ASP.NET 不知道用户关闭窗口或离开页面。

事实上,当这种情况发生时,尝试在服务器端立即执行任何操作是很棘手的。我的意思是,在 Javascript 中调用 window.onbeforeunload 并在用户关闭窗口之前调出一个确认框很容易。但是,当您尝试在窗口实际关闭之前立即向服务器发送数据包时,我认为您会发现使这一切变得用户友好成为一项艰巨的任务。您可以编写一些 Javascript,使用 AJAX 向服务器发送消息,通知服务器用户正在关闭浏览器,然后在发送该消息后使用更多 Javascript 关闭浏览器。但随后用户会收到烦人的消息(在大多数浏览器中),指出脚本正在尝试关闭窗口。对于用户来说,这看起来非常业余,并且可能会让他们在回来之前三思而后行。

因此,如果您愿意忍受延迟,我会完全避免整个 Javascript 混乱,并依赖于使用 Session_End() 的纯服务器端解决方案。毕竟,如果用户关闭窗口或离开,ASP.NET最终会注意到,因为会话将超时。

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

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.

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.

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.

不念旧人 2024-08-12 02:28:39

session_end将处理会话超时问题。但是,当用户关闭浏览器或移动到不同的 URL 时,这不会触发。它会等待,直到会话超时到期。

为了检测其他条件,您必须在客户端上使用 javascript。然而,这些方法并不总是可靠,具体取决于浏览器/版本等。此外,如果用户的计算机关闭、拔掉插头或完全消失,则该脚本将不会触发。

您可以考虑其他选择。您到底想用这个功能来完成什么?

Session_end will handle the session time out issue. However, this will not fire when the user closes the browser or moves to a different URL. It waits until the session timeout expires.

In order to detect the other conditions, you'll have to use javascript on the client. However, these methods aren't always reliable depending on the browser/version, etc. Also if the user's computer shuts down, is unplugged, or they otherwise completely go away this script won't fire.

You might consider other alternatives. What exactly are you trying to accomplish with this function?

恬淡成诗 2024-08-12 02:28:39

网络是无状态的,因此您无法知道用户是否关闭了浏览器窗口。您可以使用 global.asax 文件中的“Session_End”事件来设置会话超时。如果您想访问所有会话,您可以通过“Session_Start”事件将它们存储在一个集合中,也在全局文件中。

编辑:如果您想使用 javascript 拦截页面的关闭事件,请执行以下操作(注意,这是不可靠的):

window.onbeforeunload = closeIt;

function closeIt(){ 


  return "You have unsaved changes. Are you sure you want to leave this page?";

}

The web is stateless so you're not going to know whether the user has closed the browser window. You can use the 'Session_End' event in the global.asax file for session timeouts. If you want access to all sessions, you can store them in a collection via the 'Session_Start' event, also in the global file ..

Edit: If you want to intercept the close event of a page using javascript, here's how (note, this is unreliable):

window.onbeforeunload = closeIt;

function closeIt(){ 


  return "You have unsaved changes. Are you sure you want to leave this page?";

}
荒芜了季节 2024-08-12 02:28:39

您可以拥有一个由每个 aspx 页面和页面继承的基页。在您的基页初始化事件中,只需检查会话变量即可。如果什么都没有,则意味着会话已过期,您可以将用户重定向到登录页面。

You could have a basepage which is inherited by every aspx page & in your base page init event just check for a session variable for nothing. If it is nothing then it means session has expired and you can redirect user to a login page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文