How to save login information after closing the browser using asp.net Identity?

When using ASP.NET Identity, I want to keep login information as long as possible when the user logs into my site, so the user does not need to log in again when they open their browser again (same as github.com and stackoverflow.com) . When I log in to github, it saves my information for many days, so I don’t need to log in again every day. Are there any methods that can implement this functionality using ASP.NET Identity?

+6
source share
2 answers

Just pass the appropriate value to the isPersistent argument of the isPersistent methods:

 SignInManager.PasswordSignInAsync(" email@id.com ", "password", isPersistent: true, shouldLockout: false); 

or

 SignInManager.SignInAsync(applicationUser, isPersistent: true, rememberBrowser: false); 

The isPersistent argument isPersistent used to control the storage of the user's authentication cookie.

The rememberBrowser argument is used for two-factor authentication: a catchy browser can log into the system directly with a password.

+3
source

You need to set the IsPersistent property to AuthenticationProperties in order to continue registration after closing the browser. The registered user.GenerateUserIdentityAsync method generates a new ClaimsIdentity , which will be updated in the cookie.

 private async Task<SignInStatus> SignIn(User user, bool isPersistent) { await SignInAsync(user, isPersistent); return SignInStatus.Success; } public async Task SignInAsync(User user, bool isPersistent) { var userIdentity = await user.GenerateUserIdentityAsync(UserManager); AuthenticationManager.SignIn( new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity ); } 
0
source

All Articles