我使用 MVC 4,我的登录操作是这样的:
[HttpPost]
public ActionResult LogOn(Account loginInfo, string returnUrl)
{
if (this.ModelState.IsValid)
{
if (loginInfo.Username == "Ali" && loginInfo.Password == "110")
{
FormsAuthentication.SetAuthCookie(loginInfo.Username, loginInfo.RememberMe);
FormsAuthentication.RedirectFromLoginPage(loginInfo.Username, loginInfo.RememberMe);
}
}
this.ModelState.AddModelError("", "The user name or password provided is incorrect.");
ViewBag.Error = "Login faild! Make sure you have entered the right user name and password!";
return View(loginInfo);
}
现在我的问题是:我如何在任何时候检查用户是否选中了 RememberMe 复选框,换句话说,获取 PersistentCookie 的值?
两种解决方案:
我的解决方案:
var isPersistent = false;
var authCookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
isPersistent = ticket.IsPersistent;
}
春好汤溶液(稍加改动):
var isPersistent = ((System.Web.Security.FormsIdentity) User.Identity).Ticket.IsPersistent;