重定向后会话变量丢失

时间:2019-06-18 19:46:35

标签: asp.net-mvc-4 session-variables forms-authentication

我有一个MVC网站,该网站正在使用成员资格提供程序针对多个域进行身份验证。

我正在使用SESSION存储用户的displayName,然后将其显示在_Layout.cshtml中

这一切都可以在我的本地主机上完美运行,但是当我将应用程序部署到VM / Server时,SESSION为null,并且我将Object设置为对象的实例,错误指向布局页面中的行显示完整的用户名。会话超时后会发生这种情况,并且用户请求一个新页面并需要再次登录。

这是我的登录方法:

    if (!ModelState.IsValid)
    {
        return View(model);
    }

    Session["UserName"] = "Unknown User";
    Logger.Instance.Info("Setting username to unknown user");
    var msg = string.Empty;
    var user = _userSvc.GetLoginUserData(model.Username, out msg);
    if (!string.IsNullOrEmpty(msg)) 
    {
        Logger.Instance.Info("getting user info but error returned: "+msg);
        ModelState.AddModelError("", msg);
        return View(model);
    }

    Logger.Instance.Info("User was found "+user.EMAIL);
    foreach (MembershipProvider provider in Membership.Providers)
    {

        var username = user.LEGACY_USERNAME;
        if (provider.Name == "ADENTMembershipProvider") username = user.ENT_USERNAME;
        try
        {
            Logger.Instance.Info("Authenticating user "+username+" to "+provider.Name);
            if (provider.ValidateUser(username, model.Password))
            {
               Session["UserName"] = user.FIRST_NAME + ' ' + user.LAST_NAME;
                Logger.Instance.Info("User authenticated " + Session["UserName"]);
                var authTicket = new FormsAuthenticationTicket(
                1, // version
                user.ID.ToString(), // user name
                DateTime.Now, // created
                DateTime.Now.AddMinutes(2), // expires
                false, // persistent?
                user.ROLE // can be used to store roles
                );

                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                Response.Cookies.Add(authCookie);

                if (returnUrl != null && returnUrl != "/")
                {
                    Logger.Instance.Info("Redirecting user to " + returnUrl);
                    Response.Redirect(returnUrl);
                }                           
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Error("Error authenticating user " + username + " to " + provider.Name, ex); 
        }                
    }
    SynLogger.Instance.Info("User could not be authenticated " );
    // User was not authenticated in any of the AD
    ModelState.AddModelError("", "Invalid login attempt. The username or password provided is incorrect.");
    return View(model);             

这是我的布局中的行,当会话超时并且用户重新登录时,会引起问题:

<a href="#">Hello @Html.Raw(Session["UserName"].ToString())</a>

不确定为什么它可以在我的开发机器上运行,但是当我部署到服务器时却不能。

除了SESSION之外,还有其他方法可以存储用户名,以便我可以在布局页面中访问它吗?

0 个答案:

没有答案