我有一个通过 web.config 启用 FormsAuthentication 的 mvc 网站:
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" />
</authentication>
我的问题是当用户等待大约一分钟(显然会话超时很短)并在页面上触发操作时,所有 POST 参数都丢失了。 会发生什么:
这里是用户重定向时LoginController中的代码(这里的returnUrl参数是为了获取自动添加的参数):
[HttpGet]
public ActionResult Index(string returnUrl)
{
var model = new Models.Login.User()
{
IsAuthenticated = HttpContext.User.Identity.IsAuthenticated
};
if (model.IsAuthenticated && !string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return View(model);
}
我使用 HttpGet 是因为(我想)它是 FormsAuthentication 机制。
以及验证代码,当用户输入他的登录名和密码时,仍然在 LoginController 中:
[HttpPost]
public ActionResult Index(Models.Login.User user, string returnUrl)
{
if (ModelState.IsValid)
{
//Vérifie le ldap
using (var pc = new PrincipalContext(ContextType.Domain))
{
// validate the credentials
user.IsAuthenticated = pc.ValidateCredentials(user.UserInfo.Login, user.Password);
if (user.IsAuthenticated)
{
Code.Identity.UserInfo = Code.Identity.GetUser(user.UserInfo.Login);
FormsAuthentication.SetAuthCookie(user.UserInfo.Login, true);
if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else return Redirect("/");
}
else
ModelState.AddModelError($"{nameof(user.Password)}", "Identifiant ou mot de passe incorrect");
}
}
return View(user);
}
我想要什么:
我对 FormsAuthentication、AuthorizationAttribute 进行了大量研究(但应该在身份验证后使用),我发现实现 FormsAuthentication 的方法只有 FormsAuthentication.SetAuthCookie()。
提前感谢您的建议。