MVC Forms身份验证 - 登录后IsAuthenticated为false,显式重定向时为true

时间:2012-12-06 10:39:00

标签: asp.net-mvc cookies login forms-authentication

我有以下方法负责登录验证:

 [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel loginModel, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            //if (Membership.ValidateUser(loginModel.UserName, loginModel.Password))
            var session = RMWebClientBL.Sessions.Login(loginModel.UserName, loginModel.Password);
            if(session != null && !session.IsFailed && session.SessionId != Guid.Empty)
            {
                SetAuthCookie(loginModel, session);
                RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View();
    }

    private void SetAuthCookie(LoginModel loginModel, DomainObjects.Sessions.SessionDetails session)
    {
        // create encryption cookie         
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                loginModel.UserName,
                DateTime.Now,
                //TODO: make it configurable!!!!
                DateTime.Now.AddMinutes(20),
                loginModel.RememberMe,
                session.SessionId.ToString());

        // add cookie to response stream         
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
        System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        if (authTicket.IsPersistent)
        {
            authCookie.Expires = authTicket.Expiration;
        }
        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);  
    }

    private ActionResult RedirectToLocal(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
        {

           return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

然而,当我尝试登录“User.Identity.IsAuthenticated”在设置cookie后仍然是假的,但似乎我已登录,因为如果我正在浏览我的徽标,将我重定向到主页我已通过身份验证。 为什么我登录后无法重定向?

3 个答案:

答案 0 :(得分:2)

发现解决方案毕竟是一件愚蠢的事情: 问题出在以下方法上:

  public ActionResult Login(LoginModel loginModel, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            //if (Membership.ValidateUser(loginModel.UserName, loginModel.Password))
            var session = RMWebClientBL.Sessions.Login(loginModel.UserName, loginModel.Password);
            if(session != null && !session.IsFailed && session.SessionId != Guid.Empty)
            {
                SetAuthCookie(loginModel, session);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View();
    }

正如您所看到的,我已经添加了"返回"到

return RedirectToLocal(returnUrl);

问题在于"返回"关键字丢失所以我总是到最后一行:

return View();

这就是我总是回到我的登录页面而没有找到正确的页面。

答案 1 :(得分:0)

只是通过查看它看起来并不像你使用.NET中包含的标准MembershipProvider,因为正是这个提供程序设置了IsAuthenticated标志。您似乎使用自己的提供程序,但仅使用FormsAuthenticationTicket部分。

答案 2 :(得分:0)

尝试使用以下方法,而不是加密并创建自己的Cookie。

  FormsAuthentication.SetAuthCookie(userAuth, model.RememberMe);

该方法不只是加密票证,而且还创建一个cookie,将其添加到响应以及场景后面的其他一些事情。我在我的项目中使用它,它的工作非常好。