简体   繁体   English

如何在ASP.NET,VB.NET中解决这个会话问题?

[英]How to tackle this session problem in ASP.NET,VB.NET?

How to tackle this session problem in ASP.NET,VB.NET? 如何在ASP.NET,VB.NET中解决这个会话问题?

The following requirement are there: 有以下要求:

When the authorized user logs into the system that user is not allowed to login from another computer or in different browser other than that user is using right at this time. 当授权用户登录系统时,不允许用户从另一台计算机或其他浏览器登录,而该用户此时正在使用该浏览器。

The remedy we applied was: We have kept "Is_Loggedin" as a column with data type "bit" in a mst_vendor as a table name. 我们应用的补救措施是:我们将“Is_Loggedin”保留为mst_vendor中数据类型为“bit”的列作为表名。 When a user logs in we set the flag, Is_Loggedin, to "1" and each time when someone tries to log in using this account, the system is showing the error "The user is already logged in.". 当用户登录时,我们将标志Is_Loggedin设置为“1”,并且每当有人尝试使用此帐户登录时,系统显示错误“用户已登录。”。

When the user logs out it turns to "0" as the logout procedure calls as soon as the user clicks the log out button. 当用户注销时,一旦用户单击注销按钮,注销过程就会调用“0”。

Problem scenario: 问题场景:

  1. When the user closes the browser the flag remains the same, that is, "1". 当用户关闭浏览器时,标志保持不变,即“1”。

  2. When power gets off, it remains the same as "1". 电源关闭时,它仍然与“1”相同。

  3. If the session timeouts after a predefined value it remains the same. 如果会话在预定义值之后超时,则它保持不变。

  4. There may be different scenarios other than this. 除此之外可能有不同的场景。

Is there any way so that we can store this internal flagging for the user's login status using the application object? 有没有办法让我们可以使用应用程序对象为用户的登录状态存储此内部标记?

It may improve efficiency of the system and also eliminates the above problematic scenarios. 它可以提高系统的效率并且还消除了上述有问题的情况。

You should use the Global.asax file and use the Session_End function. 您应该使用Global.asax文件并使用Session_End函数。

Session_End: Fired when a user's session times out, ends, or they leave the application Web site . Session_End: 当用户的会话超时,结束或离开应用程序网站时触发

Store a datetime as another column next to the bit, and update it each and every time the user requests a page. 将日期时间存储为该位旁边的另一列,并在每次用户请求页面时更新它。

When a new user comes along with the same credentials and the bit is "1" you can check the datetime, and if it was a while ago you can be certain the user is no longer there. 当新用户使用相同的凭据并且位为“1”时,您可以检查日期时间,如果是一段时间之前,您可以确定用户不再存在。 So let the login go ahead. 所以让登录继续。

You could keep a pulse going in script, and when the pulse times out, consider the user finished with that session. 您可以在脚本中保持脉冲,当脉冲超时时,请考虑用户完成该会话。

The benefit to this is that you can tell the difference between the user sitting idle on the site and the user leaving the site. 这样做的好处是,您可以区分用户在网站上闲置和离开网站的用户之间的区别。

Yeah, a script would be a good idea. 是的,脚本是个好主意。 Just set the session timeout to be 5 minutes instead of 20 and then write a method into session.end in the global.asax file that updates the database accordingly. 只需将会话超时设置为5分钟而不是20分钟,然后将方法写入global.asax文件中的session.end ,该文件相应地更新数据库。

From a very top level view, here is what you can do 从最顶层的视图,您可以做到这一点

  • Use Cache with SlidingExpiration. 使用带有SlidingExpiration的缓存。

  • Everytime a user attempts login, check the cache with his username as the key. 每次用户尝试登录时,请以用户名作为密钥检查缓存。 If an entry exists in the cache, you can say that user is already logged in and deny login. 如果缓存中存在条目,则可以说该用户已登录并拒绝登录。

  • If the key is not found, allow login and create a new key in the cache as the username and set the sliding expiration time. 如果未找到密钥,则允许登录并在缓存中创建新密钥作为用户名并设置滑动到期时间。 (This should be carefully chosen as this would be the duration, the user wouldnt be locked out after the browser is closed and user attempts to relogin.) (这应该仔细选择,因为这将是持续时间,用户在浏览器关闭并且用户尝试重新登录后不会被锁定。)

  • In the Application_PreRequestHandlerExecute handler in Global, check if the user is currently active (you can use sessions for this), reset the sliding expiration time for the user. 在Global中的Application_PreRequestHandlerExecute处理程序中,检查用户当前是否处于活动状态(您可以使用会话),重置用户的滑动到期时间。 This way, with each page request the cache expiration time would be reset. 这样,对于每个页面请求,将重置缓存到期时间。

  • If the user closes the browser and moves off, the cache would expire after the set period of time, and would free the user to log in again. 如果用户关闭浏览器并离开,则缓存将在设置的时间段后过期,并将释放用户再次登录。

  • if in case the user attempts to login again before the cache expires, the user would have to wait for some time to let the cache expire. 如果用户在缓存过期之前尝试再次登录,则用户必须等待一段时间才能使缓存过期。

  • if the user logs off properly, you can remove the cache entry on the logoff event such that user doesnt have to wait to relogin. 如果用户正确注销,则可以删除注销事件上的缓存条目,以便用户不必等待重新登录。

The Sliding expiration timeout can be synced with session timeout to emulate the actual session timeout for the application. Sliding到期超时可以与会话超时同步,以模拟应用程序的实际会话超时。

With this approach, you would also save on a lot of database round trips to update/check the user status and this would work irrespective of the hosting enviornment or the session modes. 使用这种方法,您还可以节省大量数据库往返以更新/检查用户状态,无论托管环境或会话模式如何,这都可以。

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

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