1

カスタムフォーム認証チケットを設定した後、セキュリティで保護されたアクションにリダイレクトするという問題があります。起こっていることは次のとおりです。

  1. サイト/ホーム/インデックスに移動します
  2. サイト/アカウント/ログインに自動的にリダイレクトされます
  3. 有効なユーザー/パスでログインします
  4. RedirecToUrl()関数は、サイト/ホーム/インデックスにリダイレクトしようとしますが、自動的にサイト/アカウント/ログインに戻ります。
  5. リクエストは認証されています。手動でサイト/ホーム/インデックスに移動すると、で許可されます。

誰かが光を当てることができますか?

私のHomeController:

[Authorize]
public ActionResult Index()
{
    return View();
}

私のAccountController:

    [HttpGet]
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            bool bLogin = MyAuthentication.Login(model.UserName, model.Password);

            if (bLogin)
            {
                Response.Cookies.Add(MyAuthentication.GetAuthenticationCookie(model.UserName.ToLower(), model.RememberMe));
                RedirectToUrl(returnUrl);
            }
            else
                ModelState.AddModelError("", "That is not a valid Username/Password combination");

        }

        return View(model);
    }

    private ActionResult RedirectToUrl(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
            return Redirect(returnUrl);
        else
            return RedirectToAction("Index", "Home");
    }

カスタムチケットを作成する方法は次のとおりです(ユーザーデータを追加するだけです)。

    public static HttpCookie GetAuthenticationCookie(string UserName, bool persistLogin)
    {
        var userData = null; // Code removed for brevity

        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                 1,
                 UserName,
                 DateTime.Now,
                 DateTime.Now.AddMinutes(20),
                 persistLogin,
                 userData);

        string encTicket = FormsAuthentication.Encrypt(authTicket);
        return new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
    }
4

1 に答える 1

3

うーん!!!

 RedirectToUrl(returnUrl);

する必要があります

 return RedirectToUrl(returnUrl);
于 2012-11-07T19:16:07.103 に答える