カスタムフォーム認証チケットを設定した後、セキュリティで保護されたアクションにリダイレクトするという問題があります。起こっていることは次のとおりです。
- サイト/ホーム/インデックスに移動します
- サイト/アカウント/ログインに自動的にリダイレクトされます
- 有効なユーザー/パスでログインします
- RedirecToUrl()関数は、サイト/ホーム/インデックスにリダイレクトしようとしますが、自動的にサイト/アカウント/ログインに戻ります。
- リクエストは認証されています。手動でサイト/ホーム/インデックスに移動すると、で許可されます。
誰かが光を当てることができますか?
私の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);
}