会话超时
我正在使用 ASP.Net 3.5 C# 开发一个网站。我列出了我的网站中的所有在线用户(重新登录我的网站的用户)。当用户注销或只是关闭浏览器或导航到其他站点时,我想跟踪和更新数据库中的用户状态。在所有这些情况下,我想将用户的状态更新为“注销”。
我怎样才能继续前进。
谢谢 维韦克
I am developing a web site using ASP.Net 3.5 C#. I am listing all the Online users ( users who re logged in on my site) in my site. I want to track and update user's status in Database when a user has logged out or simply closed the browser or navigated to some other site. In all these cases I want to update user's status as "Logged Out".
How can i move forward with it.
Thanks
Vivek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当用户单击按钮时,您只需在服务器端(在代码隐藏中)处理单击事件,然后记录状态更改。
对于浏览器关闭的情况,可以在 global.asax 中处理 Session_End 事件,该事件在会话结束时触发:
When the user clicks the button, you can just handle the click event on the server-side (in code-behind) and then log the status change.
For the case where the browser is closed, you can handle the Session_End event in the global.asax, which fires when the session ends:
合法注销(即通过点击注销按钮等方式注销)可以轻松跟踪。您只需处理该事件并将其数据库状态标记为已注销。
然而,关闭浏览器是我从未成功完成的一件事。您将通过网络获得许多解决方案,这些解决方案会告诉您捕获关闭按钮,然后是 ajax 请求等,但我没有成功解决任何一个问题。
(像 Session_End 这样的事情可能会很方便,但有一个问题是,如果您使用除 IN-PROC 会话模式之外的任何其他模式,则该事件不会被触发,因此这是不可靠的)。
Legitimate logout (i.e. Logout by clicking on logout button etc.) can be tracked easily. You just have to handle the event and mark their database status logged out.
However closing the browser is one thing I never had a good success with. You will get many solutions over web which would tell you to capture the close button and then ajax request etc, but I did not have success with any one with that.
(Things like Session_End may come handy but there is a Gotcha that thisevent does not get fired, if you are using anything other than IN-PROC session mode so that's not reliable).
您并不真正知道用户是否关闭了浏览器,或者他是否导航到了另一个站点。我认为您需要使用某种 AJAX 控件,该控件会在给定的时间间隔内向服务器发送一些消息,以确保用户正在查看您的网站。
You don't really know if the user has closed the browser or not, or if he navigated to another site. I think you need to use some sort of AJAX control that would send some messages to the server in a given time interval to make sure the user is viewing your site.
首先检查我在另一个问题中的答案:
如果某些用户关闭浏览器、关闭计算机或类似的操作,您将无法立即关闭会话并跟踪此更改。这是通过使用会话超时来实现的。
另一种可能性是,如果用户在线,它在某个时间间隔内触发了针对服务器的某些操作,那么这将在您的服务器逻辑中实现。
注销应该很容易追踪,因为这是“人类用户”操作。只需在身份验证管理器类或任何其他处理身份验证的类中实现“UserLogout”事件并跟踪其中的注销即可。
由于技术限制,无法跟踪客户端用户操作(例如浏览到另一个页面或关闭 Web 浏览器):该领域缺乏 API。这更多是因为 Web 范式及其原理。你需要错过那个。
First check my answer in this other question:
You wouldn't be able to immediately close a session and track this change if some user closes the browser, shutdowns computer or something like that. This is achieved by playing with session timeout.
Another possibility could be consider an user online if it triggered some operation against the server in some time interval, thing that'll be implemented in your server logic.
Logging out should be easly trackable because it's an "human user" action. Just implement a "UserLogout" event in your authentication manager class or any other class handling authentication and track logouts there.
Client-side user actions like browsing to another page or closing Web browser can't be tracked because technology limitations: API lacks in this area. It's more because of Web paradigm and its principles. You'll need to miss that.