我想实现一种代码,使每个用户仅启用一个 session 。 我使用带有表单例份验证的 asp.net。
我读过这篇文章: Limit only one session per user in ASP.NET
但我想做相反的事情,断开所有以前的 session 。
这是我的代码:
void Application_Start(object sender, EventArgs e) { Application["UsersLoggedIn"] = new System.Collections.Generic.Dictionary<string, string>(); }
当用户登录时,我插入一条记录:用户名、 session ID。 如果用户名存在,我只更新sessionID
void Application_BeginRequest(object sender, EventArgs e)
{
System.Collections.Generic.Dictionary<string, string> d = HttpContext.Current.Application["UsersLoggedIn"] as System.Collections.Generic.Dictionary<string, string>;
if (d != null)
{
if (d.Count > 0)
{
string userName = HttpContext.Current.User.Identity.Name;
if (!string.IsNullOrEmpty(userName))
{
if (d.ContainsKey(userName))
{
if (d[userName] != HttpContext.Current.Session.SessionID)
{
FormsAuthentication.SignOut();
Session.Abandon();
}
}
}
}
}
但我得到空的 HttpContext.Current.User。 我也尝试了“AuthenticatedRequest”,但后来我得到了空 session 。
请问有什么想法吗?
最佳答案
如果您想访问 HttpContext.Current.User 和 Session,您应该使用 AcquireRequestState
事件。
关于c# - 如何只允许每个用户一个事件 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684989/