我通过维护当前登录用户的散列表实现了这一点,关键是用户名,值是他们的最后一次活动时间。
登录时,您只需检查该密钥的哈希表,如果存在,则拒绝登录。
当用户执行任何操作时,用时间更新散列表(如果将其作为核心页框架的一部分,这很容易)。
如果散列表中的时间大于20分钟的非活动时间,则将其删除。每次检查hashtable时都可以这样做,所以即使你只有一个用户,并且在几个小时后尝试登录,在初始检查期间,它也会将它们从哈希表中移除以便闲置。
在C#(未测试)的一些例子:
public Dictionary<String,DateTime> UserDictionary
{
get
{
if (HttpContext.Current.Cache["UserDictionary"] != null)
{
return HttpContext.Current.Cache["UserDictionary"] as Dictionary<String,DateTime>;
}
return new Dictionary<String,DateTime>();
}
set
{
HttpContext.Current.Cache["UserDictionary"] = value;
}
}
public bool IsUserAlreadyLoggedIn(string userName)
{
removeIdleUsers();
return UserDictionary.ContainsKey(userName);
}
public void UpdateUser(string userName)
{
UserDictionary[userName] = DateTime.Now;
removeIdleUsers();
}
private void removeIdleUsers()
{
for (int i = 0; i < UserDictionary.Length; i++)
{
if (user[i].Value < DateTime.Now.AddMinutes(-20))
user.RemoveAt(i);
}
}
你有没有遇到客户IP变化的问题?或者是你的环境避免了这个问题? – Owen 2008-09-25 23:31:19
我们在IP变化方面没有问题。这是我们控制IP地址的CSR的应用程序。 – 2008-09-25 23:35:39