title: Implement "Remember me" in ASP.NET CORE 3.1 MVC
body: <p>I can't figure out how to add the functionality of &quot;<strong>Remember me</strong>&quot; while logging in the website ASP.NET CORE 3.1 MVC according to the code I have below. Where and how should I check if the session on server side has expired and, in this case, load the user info from the DB according to the cookie?</p>
  <p><strong>Practical example:</strong> <em>A user logs in (with &quot;Remember me&quot; checked) and comes back on the website 1 week later. In the meantime, the session on the server has expired. I would like the user to be automatically logged in when the user comes back.</em></p>
  <p>Code executed server side when logging with &quot;Remember me&quot; checked:</p>
  <pre><code>var userClaims = new List&lt;Claim&gt;()
  {
       new Claim(&quot;id&quot;, user.Id.ToString()),
       new Claim(&quot;id_organisation&quot;, user.Id_organisation.ToString())
  };
  
  var grantMyIdentity = new ClaimsIdentity(userClaims, &quot;User Identity&quot;);
  var userPrincipal = new ClaimsPrincipal(new[] { grantMyIdentity });
  await HttpContext.SignInAsync(userPrincipal, new AuthenticationProperties
  {
         IsPersistent = true,
         ExpiresUtc = DateTime.UtcNow.AddMonths(1)                          
  });
  </code></pre>
  <p>In the Startup.cs I have:</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
       ...
       TimeSpan expiration_cookie_and_session = TimeSpan.FromHours(2);
       services.AddAuthentication(&quot;CookieAuthentication&quot;)
               .AddCookie(&quot;CookieAuthentication&quot;, config =&gt;
                {
                    config.Cookie.Name = &quot;UserLoginCookie&quot;;
                    config.LoginPath = &quot;/connexion&quot;;
                    config.SlidingExpiration = true;
                    config.ExpireTimeSpan = expiration_cookie_and_session;
                    config.EventsType = typeof(MyCookieAuthenticationEvents);
                });
       services.AddScoped&lt;MyCookieAuthenticationEvents&gt;();
       services.AddSession(options =&gt; {
                options.IdleTimeout = expiration_cookie_and_session;
           });
        ...
   }
  
  public class MyCookieAuthenticationEvents : CookieAuthenticationEvents
  {
      //We are here in case of cookie expiration
      public override Task RedirectToLogin(RedirectContext&lt;CookieAuthenticationOptions&gt; redirectContext)
      {
       ...
      }
  }
  </code></pre>
  <p>My guess would be in the CookieAuthenticationEvents.OnSigningIn event. Can you help me to make it clear?
  Thank you!!</p>