【问题标题】:ASP.NET MVC session is null. Session Variables are not being setASP.NET MVC 会话为空。未设置会话变量
【发布时间】:2015-03-11 10:39:57
【问题描述】:

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

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

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

【问题讨论】:

  • 你的调试告诉了你什么?
  • @AntP 在调试中,我有会话对象但没有设置变量。
  • @Krumelur 你能指导我吗,如果我写课程的地方真的是它的地方吗?

标签: c# asp.net-mvc asp.net-mvc-4 session


【解决方案1】:

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

    void Session_Start(object sender, EventArgs e)
    {
    }

    void Session_End(object sender, EventArgs e)
    {

    }

【讨论】:

  • 空白地实施它们并没有帮助,我只是将与会话相关的代码转移到 Session start 中,它就像一个魅力:D。谢谢
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 2013-01-27
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多