3
我似乎在理解Identity 2.0和Cookie的工作方式時遇到了一些麻煩。 ASP.NET MVC 5.用戶使用'記住我'登出
我想要的是: 如果用戶登錄並且他選中了「記住我」複選框,我不希望他永遠退出。但是會發生什麼情況是:用戶經過一定的時間後會被註銷。
如果用戶在時間範圍之前關閉瀏覽器,則「記住我」功能將起作用。 (當他再次打開該網站,他仍然登錄)。
這是我對在簽署代碼:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// Require the user to have confirmed their email before they can log on.
var user = await UserManager.FindByNameAsync(model.Email);
if (user != null)
{
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
await SendEmailConfirmationTokenAsync(user.Id);
ModelState.AddModelError("", "Gelieve eerst je e-mailadres te bevestigen.");
return View(model);
}
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Ongeldige aanmeldpoging.");
return View(model);
}
}
這是Startup.Auth代碼:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(5),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(10),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (id.GetUserId<int>()))
}
});
所以我希望用戶在5分鐘後不會被註銷,因爲isPersistent標誌在PasswordSignInAsync函數中設置。
Thanx的任何幫助。