How To Add Remember Me On Login Page In Mvc

Today, I want to share with you how to add the “Remember Me” functionality to a login page in MVC. This feature allows users to stay logged in even after closing their browser, making it convenient for returning users.

Before we proceed, I would like to mention that this feature involves storing a persistent token on the user’s machine, which some users may not be comfortable with due to privacy concerns. It is important to be transparent with your users about how their information will be stored and used.

To implement the “Remember Me” functionality, we will need to modify both the login page view and the login action method in our MVC application. Let’s start with the view.

Modifying the Login Page View

In the Login.cshtml file, we need to add a checkbox input element for the “Remember Me” option. We can do this by adding the following code inside the form element:

<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" name="RememberMe" value="true"> Remember me
</label>
</div>

Next, we need to handle this input in our login action method.

Modifying the Login Action Method

In the Login action method of the AccountController, we need to add a parameter for the “Remember Me” option:

public ActionResult Login(LoginViewModel model, bool RememberMe)

Inside the method, we can check the value of the RememberMe parameter and set the appropriate cookie for persistent login:

if (RememberMe)
{
var cookie = new HttpCookie("RememberMe", model.Email);
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}

Here, we are creating a new cookie named “RememberMe” and setting its value to the user’s email address. We also set the expiration date of the cookie to 30 days from the current date. This ensures that the user remains logged in for a long period of time.

Retrieving the Remembered User

When the user visits the login page again, we need to check if a “RememberMe” cookie exists and automatically populate the email field. To do this, we can add the following code inside the Login action method:

if (Request.Cookies["RememberMe"] != null)
{
model.Email = Request.Cookies["RememberMe"].Value;
}

With this code, we check if a cookie named “RememberMe” exists and retrieve its value. If the cookie is found, we set the email field of the login view model to the value stored in the cookie.

Conclusion

Adding the “Remember Me” functionality to a login page in MVC can greatly enhance the user experience by allowing users to stay logged in for extended periods of time. However, it is important to inform users about how their information will be stored and used to address any privacy concerns they may have.

Remember to always prioritize the security of user data when implementing such features. Keep in mind that there may be legal and ethical considerations when storing and managing user information. If in doubt, consult with legal professionals to ensure compliance with applicable regulations.

Now that you have learned how to add the “Remember Me” functionality to a login page in MVC, you can provide a seamless and convenient user experience for your application’s users.