使用ASP.NET MVC3和基于表单的登录。 登录过程非常有效。但是当我关闭浏览器并返回页面时,它会进入登录页面,我需要再次输入凭证。我怎么能避免这种情况。
这是我的登录代码:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.Username, model.Password))
{
var userprofile = svc.GetUserByUsername(model.Username);
var serializeModel = new CustomPrincipalSerializeModel();
serializeModel.Username = userprofile.Username;
serializeModel.Name = userprofile.Name;
serializeModel.DisplayAs = userprofile.DisplayAs;
serializeModel.MarketId = userprofile.MarketId;
var serializer = new JavaScriptSerializer();
var userData = serializer.Serialize(serializeModel);
//FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
var authTicket = new FormsAuthenticationTicket(
1,
model.Username,
DateTime.Now,
DateTime.Now.AddDays(30),
true,
userData);
var encTicket = FormsAuthentication.Encrypt(authTicket);
var faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Användarnamnet eller lösenordet är felaktigt.");
}
}
return View(model);
}
答案 0 :(得分:2)
您需要使用持久性Cookie(在您的情况下设置faCookie.Expires
,因为您手动创建了Cookie。)
有关详细信息,请参阅this KB article。