ASP.NET MVC 会话为空.未设置会话变量

本文关键字:会话 设置 变量 NET MVC ASP | 更新日期: 2023-09-27 18:37:24

我的 Global.asax 中有以下代码

protected void Application_AcquireRequestState(object sender, EventArgs e)
{           
    if (HttpContext.Current.Handler is IRequiresSessionState)
    {
        if (Request.IsAuthenticated)
        {
            if (HttpContext.Current.Session[this.MemberShipProvider.PrincipalSessionKey] != null)
            {
                CxPrincipal principal;
                try
                {
                    principal = (CxPrincipal)HttpContext.Current.Session[this.MemberShipProvider.PrincipalSessionKey];
                }
                catch
                {
                    principal = null;
                }
                HttpContext.Current.User = principal;
                Thread.CurrentPrincipal = principal;
            }
            else
            { 
                var identity = new CxIdentity("admin", 1, "", true);
                CxPrincipal principalLogin = new CxPrincipal(identity, 1);
                HttpContext.Current.Session[this.MemberShipProvider.PrincipalSessionKey] = principalLogin;
                HttpContext.Current.Session[SessionName.CurrentUser] = "Admin User";
                HttpContext.Current.User = principalLogin;
                Thread.CurrentPrincipal = principalLogin;
                this.FormServiceProvider.SignIn("admin", false); // this is equal to FormsAuthentication.SetAuthCookie
            }
        }
    }
}

问题是每次会话为对象时都是空的。不仅在这里,我也不能在我的应用程序中使用会话。会话正在重置或类似的东西。

我的应用程序不再需要用户登录。因此,我没有在我的应用程序中使用Session.Clear或Session.Abandon

请帮助我,为什么我的会话变量没有设置?

ASP.NET MVC 会话为空.未设置会话变量

您需要在global.asax.cs中实现(并留空)2 种方法:

    void Session_Start(object sender, EventArgs e)
    {
    }
    void Session_End(object sender, EventArgs e)
    {
    }