简体   繁体   English

用户关闭浏览器时的“注销”事件

[英]The “Logging out” event when a user closes the browser

I have a table called Eventlog, this table contains already data about user connection: when the user calls this action for example : 我有一个名为Eventlog的表,该表包含有关用户连接的数据:当用户调用此操作时,例如:

public ActionResult Login(string username, string password)
{

}
  • I test whether the user already exists on database or not 我测试用户是否已经存在于数据库中

  • If yes, I use Session["user"] = username or FormsAuthentication.SetAuthCookie(username, true); 如果是,我使用Session["user"] = usernameFormsAuthentication.SetAuthCookie(username, true); to set the user session 设置用户会话

  • And then I put a record on the Eventlog table : user X was connected at Y o'clock 然后我在Eventlog表上放了一条记录: 用户X在Y点连接

This works fine, but I want also the information about the user logging out. 这工作正常,但我也想要有关用户注销的信息。 I can do similar thing to the LogOff Action, I guess it is gonna work fine as well, but the majority of people don't use the logoff button, they only close the browser, how is it possible to implement the user logoff event for this situation when the user closes the browser: user X has been disconnected at Y o'clock . 我可以对LogOff Action做类似的事情,我想它也会正常工作,但是大多数人不使用注销按钮,他们只关闭浏览器,如何实现用户注销事件用户关闭浏览器时的情况: 用户X已在Y时断开连接 The Session_End() does not serve the need in this situation. 在这种情况下,Session_End()不能满足需要。

You have to accept the limitations of web technology. 您必须接受Web技术的限制。 Once you have sent your response to the user agent, the server has no way to know what is happening with the request. 将响应发送给用户代理后,服务器无法知道请求发生了什么。 The user might close the user agent gracefully. 用户可以正常关闭用户代理。 The UA might crash. UA可能会崩溃。 The user might lose internet connection. 用户可能会丢失互联网连接。 His computer can crash. 他的电脑会崩溃。 All of this can happen before the client even receives the response. 所有这一切都可以在客户端收到响应之前发生。 This is the environment you are dealing with. 这是您正在处理的环境。 Embrace it instead of fighting it. 拥抱它而不是战斗它。

If tracking logoff is important to you, there are several techniques you might use: 如果跟踪注销对您很重要,您可以使用以下几种技术:

  1. Rely on the session timeout. 依靠会话超时。 If you choose a timeout short enough it might be enough to meet your security requirements. 如果您选择足够短的超时,则可能足以满足您的安全要求。 I would consider this the preferred way, because it is simple and proven. 我认为这是首选方式,因为它很简单并且经过验证。
  2. Use scripting to send a heartbeat from the UA to the server. 使用脚本将心跳从UA发送到服务器。 You can use "ping" requests, long calls etc. However, be aware of the performance impact this comes with, the number of requests to the server and the complexity of the implementation. 您可以使用“ping”请求,长时间调用等。但是,请注意它带来的性能影响,对服务器的请求数量以及实现的复杂性。
  3. Use an existing framework such as SignalR to establish a client-to-server connection and have the client check in to the server. 使用SignalR等现有框架建立客户端到服务器的连接,并让客户端签入服务器。 This is basically the second option with less manual work for you. 这基本上是第二个选项,为您减少了手动工作。

All of this wouldn't let you intercept user logoff or loss of connection, but if the client stops responding you know that the connection is interrupted (in one of many possible ways). 所有这一切都不会让你拦截用户注销或丢失连接,但如果客户端停止响应,你知道连接被中断(以多种可能的方式之一)。 So you shouldn't register this as "user logged off", but rather as "user disconnected". 因此,您不应将此注册为“用户注销”,而应注册为“用户已断开连接”。

How is it possible to implement that ? 怎么可能实现呢? checking every 1hour if the SessionId exists or not @Mehdi 如果SessionId存在,则每1小时检查一次@Mehdi

If you do a post the last time the user does a new action with something like: 如果您上次使用以下内容执行新操作时发布帖子:

if(Session["UserName"] != null) {
      /*Update the database with the last time that the user has performed a action*/
}

If you do every time the user goes to a new action, you will get the last time the user did something that has inpact on the server. 如果每次用户执行新操作时都会执行此操作,则最后一次用户执行了在服务器上执行了某些操作的操作。 Then you know the (not exact but a indicator) last time the user wasn't logged off. 然后,在上次用户未注销时,您知道(不完全是指示符)。

It's not possible on the server side to know when a user closes the browser. 在服务器端无法知道用户何时关闭浏览器。 However, you can use JavaScript to trigger on the window.close and send an AJAX call to the server, therefore recording when a user leaves. 但是,您可以使用JavaScript在window.close上触发并向服务器发送AJAX调用,从而在用户离开时进行记录。 This is unreliable, however, since the user has control over browser settings, and could disable JS. 然而,这是不可靠的,因为用户可以控制浏览器设置,并且可以禁用JS。 It's probably the best you can do. 这可能是你能做的最好的事情。

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

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