0

ログイン認証を担当する次の方法があります。

 [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 を設定した後も false ですが、ロゴをクリックしてホームページにリダイレクトすると認証されているため、ログインしているように見えます。ログイン後にリダイレクトできないのはなぜですか?

4

3 に答える 3

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」キーワードが欠落しているため、常に最後の行に到達することでした:

return View();

そのため、正しいページにアドレス指定していないときに、常にログイン ページに戻っていました。

于 2012-12-06T11:19:08.373 に答える
0

暗号化の代わりに次の方法を使用して、独自の Cookie を作成してみてください。

  FormsAuthentication.SetAuthCookie(userAuth, model.RememberMe);

このメソッドは、チケットを暗号化するだけでなく、Cookie を作成し、それを応答に追加するなど、舞台裏でいくつかのことを行います。私は自分のプロジェクトでこれを使用していますが、完全に正常に動作しています。

于 2012-12-06T11:02:50.527 に答える
0

IsAuthenticated フラグを設定するのはこのプロバイダーであるため、.NET に含まれている標準の MembershipProvider を使用しているようには見えません。独自のプロバイダーを使用しているように見えますが、FormsAuthenticationTicket 部分のみを使用しているだけです。

于 2012-12-06T10:42:57.790 に答える