c# - 如何只允许每个用户一个事件 session

标签 c# asp.net session authentication global-asax

我想实现一种代码,使每个用户仅启用一个 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/

相关文章:

c# - 将 SQL Server 错误返回给用户

C# 客户端和服务器无法通信,因为它们不具备通用算法

asp.net - asp net 4 应用程序中的 scriptresource.axd 404 错误 - webresource 工作正常

javascript - Page_isValid 更改时调用 javascript 函数?

asp.net - <location path ="web.config"> 是做什么用的?

oracle - 如何杀死用户的所有事件和非事件 oracle session

PHP session 与类

c# - Azure Function,返回状态码+JSON,无需在每个逻辑部分定义返回

C# 对具有相同类型的不同属性重新使用 LINQ 表达式

session - 在grails中使用范围变量有哪些选择?