Back to: ASP.NET MVC Tutorial For Beginners and Professionals
How to Implement Remember Me Feature in ASP.NET Identity
In this article, I am going to discuss How to Implement Remember Me Feature or How it is Implemented in ASP.NET Identity. Please read our previous article where we discussed Login a User in ASP.NET Identity.
What is Remember Me Feature in Website Login?
When you log in to a specific website, sometimes your web browser will ask if you want it to remember the password for that site, or it may say “Remember me?” meaning “Do you want this site to hold on to your login credentials?” If you click for the browser to remember your password or to remember you, the browser will store your login information and autofill the login fields each time you go to access that specific site.
The remember-me feature typically works by generating a unique cookie, associating it with the user in the database, and adding a persistent cookie (i.e. a cookie that is saved on disk by the browser) to the response once the user is logged in.
How to Implement Remember Me Feature in ASP.NET Identity
Nowadays whenever you visit any website and whenever you log into the website, then you will see a checkbox saying Remember Me something as shown in the below image.
In order to implement the Remember Me feature in ASP.NET Identity, we use the isPersistent property of the PasswordSignIn method when we are log-in to the application. All we have to do is to set the isPersistent property in the PasswordSignIn method to true.
SignInStatus result = SignInManager.PasswordSignIn(model.Email, model.Password, isPersistent: true, shouldLockout: false);
SignInStatus result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, isPersistent: true, shouldLockout: false);
Instead of hardcoding the value to true, in real-time applications, we need to pass the RememberMe value to the Action method using an HTML checkbox from a view. In that case, if the user wants the browser and server to remember the password, then he needs to check the RememberMe check else he needs to uncheck the RememberMe checkbox. And instead of hardcoding the isPersistent value, we need to set the value as follows.
SignInStatus result = SignInManager.PasswordSignIn(model.Email, model.Password, model.RememberMe, shouldLockout: false);
SignInStatus result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
How to Set the Expiration Time?
In ASP.NET Identity, by 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 behavior, go to App_Start > Startup.Auth.cs and set the value of the ExpireTimeSpan property depending on your requirements. As you can see in the below code, we have set ExpireTimeSpan = TimeSpan.FromDays(30) i.e. 30 days.
In the next article, I am going to discuss How to Get the Current Logged in User Id in ASP.NET Identity. Here, in this article, I try to explain How the Remember me Feature is Implemented in ASP.NET Identity. I hope you enjoy this Remember me Feature Implemented in the ASP.NET Identity article.