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

ASP.NET MVC session is null. Session Variables are not being set

我的 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 对象都是空的。不仅在这里,我也不能在我的应用程序中使用会话。会话正在重置或类似的东西。

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

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

您需要在 global.asax.cs 中实施(和 m.b。留空)2 个方法:

    void Session_Start(object sender, EventArgs e)
    {
    }

    void Session_End(object sender, EventArgs e)
    {

    }