title: Adding and accessing claims in asp net core 3.0 using built in Identity server
body: <p>I'm currently failing at wrapping my head around claims. I have a ASP.Net Core 3 project with the angular template and users stored in app. </p>
  
  <p>I want to add claims to my users, reading up on I thought it would be easy, just add something along the lines of </p>
  
  <pre><code>await _UserManager.AddClaimAsync(user, new Claim(AccountStatic.ClaimTypes._Claim_Id, user.Id));
  </code></pre>
  
  <p>When you create the user, and then get it back using the below line once they are logged in again:</p>
  
  <pre><code>User.FindFirst(AccountStatic.ClaimTypes._Claim_Id)?.Value;
  </code></pre>
  
  <p>This does however not work. I can see the claims being written to <code>AspNetUserClaims</code> table in my database but it's not there in the users claims when they log in. There are a few other claims there, but not the ones I have added.</p>
  
  <p>Do I need to define somewhere which of the users claims get included when they log in?</p>
  
  <p><strong>Edit.</strong> 
  I found a post stating that I need to add claims using a DI AddClaimsPrincipalFactory. So I added this class.</p>
  
  <pre><code>public class UserClaimsPrincipalFactory : UserClaimsPrincipalFactory&lt;ApplicationUser&gt;
  {
      public UserClaimsPrincipalFactory(UserManager&lt;ApplicationUser&gt; userManager,IOptions&lt;IdentityOptions&gt; optionsAccessor): base(userManager, optionsAccessor)
      {}
  
      //https://levelup.gitconnected.com/add-extra-user-claims-in-asp-net-core-web-applications-1f28c98c9ec6
      protected override async Task&lt;ClaimsIdentity&gt; GenerateClaimsAsync(ApplicationUser user)
      {
          var identity = await base.GenerateClaimsAsync(user);
          identity.AddClaim(new Claim(AccountStatic.ClaimTypes.Claim_Id, user.Id ?? "[no id]"));
          return identity;
      }
  }
  </code></pre>
  
  <p>And if I step through the code I can see the claims being added here. But in the Controller my custom claims are not present.</p>
  
  <pre><code>internal string GetUserId()
  {
      if (User.Identity.IsAuthenticated == false)
          return null;
  
      return User.FindFirst(AccountStatic.ClaimTypes.Claim_Id)?.Value;
  }
  </code></pre>
  
  <p><strong>Update.</strong> Ok I find this very strange. I have been trying to do what others claim work but for me nothing gets me the users name or id. inspecting the <code>User</code> I get the following. Nothing here contains any reference to the logged in user. </p>
  
  <p><a href="https://i.stack.imgur.com/K0ghL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K0ghL.png" alt="enter image description here"></a></p>
  
  <p><strong>Update 2:</strong>
  Just noticed that there is actually an Id in there: <code>{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: ed107a11-6c62-496b-901e-ed9e6497662a}</code> Seems to be the users id from the database. Not sure how to access it yet though. </p>
  
  <p>These return null.</p>
  
  <pre><code>User.FindFirst(JwtRegisteredClaimNames.NameId)?.Value;
  User.FindFirst("nameidentifier")?.Value;
  User.FindFirst("NameIdentifier")?.Value;
  </code></pre>
  
  <p><strong>Another update</strong>
  I'm using a <code>UserClaimsPrincipalFactory</code> and breakingpointing it and looking at the Claims I can see that all of the ones I want are there. But again, these are not available in my API controllers as seen in the first picture.
  <a href="https://i.stack.imgur.com/vZL4E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vZL4E.png" alt="enter image description here"></a></p>