title: Save tokens in Cookie with ASP.NET Core Identity
body: <p>I want to save something inside my 'Identity' generated cookie. I'm currently using the default Identity setup from the Docs.</p>
  
  <p>Startup.cs</p>
  
  <pre><code>services.Configure&lt;IdentityOptions&gt;(options =&gt;
  {
      // User settings
      options.User.RequireUniqueEmail = true;
  
      // Cookie settings
      options.Cookies.ApplicationCookie.AuthenticationScheme = "Cookies";
      options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(1);
      options.Cookies.ApplicationCookie.SlidingExpiration = true;
      options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
      options.Cookies.ApplicationCookie.LoginPath = "/Account";
      options.Cookies.ApplicationCookie.LogoutPath = "/Account/Logout";
  });
  </code></pre>
  
  <p>AccountController.cs</p>
  
  <pre><code>var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, true, true);
  
  if (result.Succeeded)
  {
      _logger.LogInformation(1, "User logged in.");
  
      var tokens = new List&lt;AuthenticationToken&gt;
      {
          new AuthenticationToken {Name = "Test", Value = "Test"},
      };
  
  
      var info = await HttpContext.Authentication.GetAuthenticateInfoAsync("Cookies");
      info.Properties.StoreTokens(tokens);
  </code></pre>
  
  <p>It seems this doesn't work. Because the cookie isn't created yet. The 'Info' variable is empty.</p>
  
  <p>I could solve it by using the 'CookieMiddleware'</p>
  
  <p>Startup.cs</p>
  
  <pre><code>app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
      AuthenticationScheme = "Cookies",
      ExpireTimeSpan = TimeSpan.FromHours(1),
      SlidingExpiration = true,
      AutomaticAuthenticate = true,
      LoginPath = "/Account",
      LogoutPath = "/Account/Logout",
  });
  </code></pre>
  
  <p>But than I need to use </p>
  
  <pre><code>await HttpContext.Authentication.SignInAsync("Cookies", &lt;userPrincipal&gt;);
  </code></pre>
  
  <p>In this case I need to build myself a 'user principal'. And I prefer to leverage 'Identity' for this matter.</p>
  
  <p>So is it possible to combine this? 
  If this is not the case how do I generate the claimsprincipal on a good way.</p>
  
  <p>Without the need to 'map' every claim.</p>
  
  <pre><code>List&lt;Claim&gt; userClaims = new List&lt;Claim&gt;
  {
      new Claim("UserId", Convert.ToString(user.Id)),
      new Claim(ClaimTypes.Name, user.UserName),
      // TODO: Foreach over roles
  };
  
  ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
  await HttpContext.Authentication.SignInAsync("Cookies", principal);
  </code></pre>
  
  <p>So something like:</p>
  
  <pre><code>ClaimsPrincipal pricipal = new ClaimsPrincipal(user.Claims); 
  </code></pre>
  
  <p>This doesn't work because user.Claims is of type IdentityUserClaim and not of type Security.Claims.Claim.</p>
  
  <p>Thanks for reading.
  Have a good day,</p>
  
  <p>Sincerely, Brecht </p>