您如何确保每个帐户一次只有一个用户登录?
假设 cookie 是持久的。
通常,最好的方法是在提供程序上实现自定义以检查上次登录,以及在代码中添加方法以跟踪用户操作。
关键是你必须知道用户最后一次做某事或注销是在什么时候。从那里您可以确定帐户是否实际准备就绪。如果您在代码中设置了对这些元素的跟踪,则可以修改成员资格提供程序以检查以确保帐户可以登录。
从概念上讲,您必须决定要如何响应。如果您让用户 A 登录,然后用户 B 尝试登录(使用相同的凭据),您会:
或者
(2) 是有问题的,因为您需要可靠地知道用户 A 已注销以确定是否登录用户 B。用户 A 可能只是在您网站上的某个页面上查看了一段时间,因此通过时间进行操作可能不是最好的. 也许某种 AJAX 看门狗每 30 秒 ping 一次您的网站。
(1) 还需要一些工作。当用户登录时,您可能希望存储他们的 cookie 值(可能在数据库中)并将其添加到为该用户发布的 cookie 列表中。这样,只会接受最后一次发出的 cookie(最后一次登录)。如果您看到较早的 cookie 之一,那么您将注销该人。
我用用户的 id 创建一个表。每次用户登录时,系统都会创建一个唯一的 id,该 ID 与用户 ID 相对应存储在表中,以及会话变量中。
Every post back or page process, the key stored in the session variable is checked against the last key for that user stored in the database. If a second user logs in with the same account, the system will create a new unique id, so that when the first user posts back or changes pages, the key stored in his session variable will be different to that in the table, indicating that the account has been opened in another location.
I schedule a night process to delete the old ids from the table