How to implement "Remember Me" option in a login page?

winanjaya 146 Reputation points
2023-12-18T15:22:20.59+00:00

Hi,

I want to have "remember me" option in my login page, how to implement it?

any example that I can follow?

Environment:

ASP MVC NetCore 6

thanks

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Qing Guo - MSFT 896 Reputation points Microsoft External Staff
    2023-12-19T03:41:25.5366667+00:00

    Hi @winanjaya ,

    You can add a RememberMe property to your model, like:

    public class User
    {
        ...
        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }
    
    
    

    Then in your login page add "remember me" option, like:

     <div class="form-group">
         <div class="checkbox">
             <label>
                 <input asp-for="RememberMe" /> @Html.DisplayNameFor(model => model.RememberMe)
             </label>
         </div>
     </div>
    

    At last , in your Login post HttpContext.SignInAsync method add :

    new AuthenticationProperties
    {
        IsPersistent = user.RememberMe
    });
    
    
    
    

    My test code like:

    [HttpPost]
    public async Task<IActionResult> Login(User model)
    {
        if (ModelState.IsValid)
        {
            User user = await _context.Users
                .FirstOrDefaultAsync(u => u.Email == model.Email && u.Password == model.Password);
    
            if (user != null)
            {
                user.RememberMe = model.RememberMe;
                await Authenticate(user);
                return RedirectToAction("Index", "HomePage");
            }
            else
            {
                ModelState.AddModelError("", "error");
            }
        }
        return View(model);
    }
    private async Task Authenticate(User user)
    {
        var claims = new List<Claim>
        {
            new Claim(ClaimsIdentity.DefaultNameClaimType, user.Email),
            new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role)
        };
    
        ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
            ClaimsIdentity.DefaultRoleClaimType);
     
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id),
             new AuthenticationProperties
             {
                 IsPersistent = user.RememberMe
             });
    }
    
    
    

    The HomePage like:

    @if (User.Identity.IsAuthenticated )
    {
        <a style="color: green">Hello @User.Identity?.Name!</a>
    
    }
    

    Result:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Qing Guo

    1 person found this answer helpful.

  2. AgaveJoe 30,126 Reputation points
    2023-12-18T15:56:00.53+00:00

    Typically, an MVC application uses cookie authentication to cache the user's identity. Set the auth cookie's AuthenticationProperties property IsPersistent = true.

    Use cookie authentication without ASP.NET Core Identity

    0 comments No comments

  3. SurferOnWww 4,631 Reputation points
    2023-12-19T01:12:07.3733333+00:00

    What is the authentication system you use? If it is the ASP.NET Core Identity the login page includes the "remember me" option. Is this not what you need?

    RememberMe

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.