Introduction
In order to implement the remember me feature in ASP.NET Identity, we use the isPersistent property.
isPersistent Usage
All you have to do is to set the isPersistent property in the PasswordSignIn method to true.
var result = SignInManager.PasswordSignIn(model.Email, model.Password, model.RememberMe, shouldLockout: false);
In a real case scenario, you can pass the RememberMe
value to your Action
using an HTML checkbox.
Expiration time
Be default, the expiration time of the authentication cookie of a logged in user is set to 14 days. After this interval, the cookie will be automatically destroyed and the user will have to re-login.
In order to change this default behaviour, go to App_Start > Startup.Auth.cs and set the value of the ExpireTimeSpan property depending on your requirements.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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>
(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
//Set the lifetime duration of the authentication cookie to 30 days
ExpireTimeSpan = TimeSpan.FromDays(30)
});