当表单字段未经验证时,通过单击提交按钮,来自浏览器的ReturnUrl消失了。
单击提交按钮之前,浏览器看起来像。
单击提交按钮后(直到表单字段不被验证),浏览器看起来像。
在单击提交按钮后如何与登录后保持相同的浏览地址,应该将其重定向到该页面。
这是动作代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
model.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30), // value of time out property
false, // Value of IsPersistent property
String.Empty,
FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl ?? Url.Action("Index", "Home"));
}
else
{
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View();
}
}
else
{
return View(model);
}
}
提前致谢
假设您查看了
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { ...
然后,当验证失败时,您需要在操作方法中重新分配值
if (ModelState.IsValid)
{
....
}
else
{
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
ViewBag.ReturnUrl = returnUrl; // Add this line
return View();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句