登录后,用户不会返回到请求的 URL。
这是控制器:
public ActionResult LogIn()
{
return View();
}
[HttpPost]
public ActionResult LogIn(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (UserIsValid(model.Username, model.Password))
{
if (model.RememberMe == false)
{
}
else
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
model.Username,
DateTime.Now,
DateTime.Now.AddDays(15),
false,
"user",
FormsAuthentication.FormsCookiePath);
string HashedCoockies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashedCoockies);
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
else
{
ModelState.AddModelError("", "The user name or password is incorrect.");
}
}
return View(model);
}
看法:
@using (Html.BeginForm())
{
<label for="username">
Username:</label>
<br />
@Html.TextBoxFor(model => model.Username)
<br />
<br />
<label for="password">
Password:</label>
<p>
<a href="#">Forgot your password?</a></p>
@Html.PasswordFor(model => model.Password)
<br />
<br />
<div id="lower">
<br />
<br />
@Html.CheckBoxFor(model => model.RememberMe, new { style = "margin-left: 10px; margin-top: 7px; position: absolute;" })
<label for="check" id="keep" style="margin-left: 30px; margin-top: -14px;">
Keep me logged in</label>
<input type="submit" value="Login" class="round-button" style="float: right; margin-right: 20px;" />
</div>
}
和web.config
<authentication mode="Forms">
<forms loginUrl="~/account/login" timeout="2880" />
</authentication>
ps 我的 login.cshtml 没有 _Layout.html