我使用以下代码跨页面检索登录用户身份
// login method of account model
public ActionResult Login(LoginModel model)
{
Session["username"]=model.Username;
//redirect to login controler
}
并在登录控制器中
public ActionResult LoginLayout()
{
if(IsAdmin(Session["username"]))
return View();
else
return OtherView();
}
一切都很好,直到我关闭浏览器然后重新打开它,然后我总是被重定向到,OtherView()
即使我仍然被认证为登录用户。
更新
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
Session["username"] = model.UserName;
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}