ASP.NET MVC会话为null.没有设置会话变量

pro*_*mar 6 c# asp.net-mvc session asp.net-mvc-4

我在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

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是Session每次对象都是null.不仅在这里,我也不能在我的应用程序中使用会话.会话正在重置或类似的事情.

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

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

Ser*_*gio 3

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

    void Session_Start(object sender, EventArgs e)
    {
    }

    void Session_End(object sender, EventArgs e)
    {

    }
Run Code Online (Sandbox Code Playgroud)