Asp.net 在MVC中登录后检索会话

Asp.net 在MVC中登录后检索会话,asp.net,asp.net-mvc,asp.net-mvc-4,session,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Session,我想使用会话创建一个购物车。我要做的是使用一个基本控制器,在我的会话[“cart”]中分配一个新的cart 正如预期的那样,当我浏览不同的页面并返回到我的“购物车”页面时,购物车中仍然存在产品 但一旦我登录,我的会话就为空。如何在登录后使会话保持活动状态,或将以前的会话数据分配给新会话 我的BaseController类: protected override void Initialize(RequestContext requestContext) { base.I

我想使用会话创建一个购物车。我要做的是使用一个基本控制器,在我的会话[“cart”]中分配一个新的cart

正如预期的那样,当我浏览不同的页面并返回到我的“购物车”页面时,购物车中仍然存在产品

但一旦我登录,我的会话就为空。如何在登录后使会话保持活动状态,或将以前的会话数据分配给新会话

我的BaseController类:

 protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        if(Session["cart"] == null)
        {
            Session["cart"] = new Order();
        }
    }
My Global.asax文件:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    protected void Application_AuthenticateRequest()
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    var id = (FormsIdentity)HttpContext.Current.User.Identity;
                    var ticket = (id.Ticket);
                    ticket = FormsAuthentication.Decrypt(id.Ticket.Name);

                    if (!string.IsNullOrEmpty(ticket.UserData))
                    {
                        string userData = ticket.UserData;
                        string[] roles = userData.Split(',');
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
                    }
                }
            }
        }
    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

我觉得在这个global.asax文件中有一些事情要做,但我不太清楚是什么。谢谢你的帮助

在会话中尝试初始化会话[“购物车”]请检查此项:您做错了什么,因为登录后不应重置会话。检查
web.config
在会话中尝试初始化会话[“购物车”]请检查此项:您做错了什么,因为登录后不应重置会话。请检查
web.config