title: ASP.NET web api "Remember Me" functionality using cookies
body: <p>I am trying to implement a <em>"Remember Me"</em> functionality in my <strong>Web Api</strong> project.</p>
  
  <p>I would like to :</p>
  
  <ul>
  <li>have the <strong>Remember Me</strong> functionality when the user <em>Sign In</em>.</li>
  <li>save a <strong>cookies</strong> for to keep the user always logged in, so that the user no need type the <em>username</em> and <em>password</em> every single time when they visit the websites.</li>
  <li>Sign the user in by reading the <strong>cookies</strong> that saved on the last login.</li>
  </ul>
  
  <p>One more question that I am thinking about is... I am trying to generate the <strong>cookies</strong> by using <code>JavaScript</code> when the user checked the <em>Remember Me</em> Checkbox. Is it possible to do this? </p>
  
  <p>OR</p>
  
  <p>I should implement the <code>RememberMe()</code> in the <code>AccountController</code>??</p>
  
  <p>Addition:
  Here's my code in <code>ApplicationOAuthProvider</code>.</p>
  
  <pre><code>    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
      {
          var userManager = context.OwinContext.GetUserManager&lt;ApplicationUserManager&gt;();
  
          ApplicationUser user = await userManager.FindByNameAsync(context.UserName);
  
          if (user == null) {...}
  
          if (userManager.IsLockedOut(user.Id)) {...}
  
          if (!(await userManager.CheckPasswordAsync(user, context.Password)))
          { ... }
  
          if (!user.EmailConfirmed) {...}
  
          ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
             OAuthDefaults.AuthenticationType);
          ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
              CookieAuthenticationDefaults.AuthenticationType);
  
              AuthenticationProperties properties = CreateProperties(user.UserName);
          AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
          context.Validated(ticket);
          context.Request.Context.Authentication.SignIn(cookiesIdentity);
  </code></pre>
  
  <p>In my JavaScript.</p>
  
  <pre><code>$('#checkbox').click(function () {
  
      if ($('#checkbox').is(':checked')) {
          // save username and password
          username = $('#txtLoginEmail').val();
          password = $('#pass').val();
          checkbox = $('#chkRememberMe').val();
      } else {
          username = '';
          password = '';
          checkbox = '';
      }
  });
  </code></pre>