我在设置自定义表单身份验证票证后重定向到安全操作时遇到问题。这是正在发生的事情:
- 我导航到站点/主页/索引
- 我被自动重定向到站点/帐户/登录
- 我使用有效的用户/密码登录
- RedirecToUrl() 函数尝试将我重定向回站点/主页/索引,但我会自动返回到站点/帐户/登录
- 该请求已通过身份验证。如果我手动导航到站点/主页/索引,则允许我进入。
任何人都可以解释一下吗?
我的家庭控制器:
[Authorize]
public ActionResult Index()
{
return View();
}
我的帐户控制器:
[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);
}