title: How Claims are mapped in asp.net core
body: <p>I am new to asp.net core and I am currently working in the multi-tenant application.I am now stuck because i have no idea of how the claims are mapped in asp.net core</p>
  
  <p><a href="https://i.stack.imgur.com/LNHjG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LNHjG.png" alt="image 1"></a></p>
  
  <p>I am getting the above error when in try to access the claims from <code>MIdAuthorizedBaseApiController</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/CR4Wp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CR4Wp.png" alt="image 2"></a></p>
  
  <p>Getting claims from <code>UserController</code>.</p>
  
  <p>My Doubt is when the claims in jwt token are mapped to "User.Claims".I am getting the claims in <code>"UserController.cs"</code> .But i need those claims in <code>MIdAuthorizedBaseApiController.cs</code> inorder to set the "<code>UserContext"</code>.</p>
  
  <p>The claims are available in <code>UserController</code> but the <code>UserController</code> itself is inheriting from <code>MIdAuthorizedBaseApiController</code>,but the Claims are not populated there.When i try to access it throws an exception.So, when and how the claims in jwt token are mapped.Please refer the two screenshots above to get my question correctly.</p>
  
  <p>UserController.cs</p>
  
  <pre><code>[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
  public class UsersController : MIdAuthorizedBaseApiController
  {
       [HttpGet]
      [ProducesResponseType(200, Type = typeof(BusinessEntities.User))]
      [ProducesResponseType(400)]
      [ProducesResponseType(401)]
      public IActionResult Users()
      {
          // Get Tenant Id
          var claimsList = User.Claims.ToList();
          var tenantId = claimsList[4].Value;
          // Set the Claims
          // _claimsHelper.SetClaims(User.Claims.AsQueryable());
  
          var users = _userManager.Users.Include(u =&gt; u.UserRoles).ThenInclude(ur =&gt; ur.Role);//.Where(n =&gt; n.TenantId.ToString() == tenantId);
  
          // Using "LINQ" to Query only the required properties
          var result = from User in users
                       select new
                       {
                          Id = User.Id,
                          UserName = User.UserName,
                          Email = User.Email,
                          PhoneNumber = User.PhoneNumber,
                          // Roles = String.Join(",", (from roles in User.UserRoles select roles.Role.Name))
                       };
  
          // Return the result set
          if(result != null) {
              return Ok(result);
          }
  
          return BadRequest(_localizer["2006"]);
  }
  </code></pre>
  
  <p>MIdAuthorizedBaseApiController.cs</p>
  
  <pre><code>[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
  public class MIdAuthorizedBaseApiController : AuthorizedApiController
  {
      public MIdAuthorizedBaseApiController(UserContext usercontext):base(usercontext)
      {
          PopulateUserContext(usercontext);
      }
  
      private void PopulateUserContext(UserContext usercontext)
      {
          // Getting exception here when i try to populate User.Claims 
          //here
           var claim = HttpContext.User.Claims.First(c =&gt; c.Type == "UserId");
  
          //To be replaced with details from token
          //usercontext.UserId = Guid.Parse("3C5CD705-8DA0-4536-856B-9F39A6ABC0FA");
          usercontext.UserId = Guid.Parse("43200003-6972-4849-B80F-81896C3B5505");
          usercontext.Username = Guid.NewGuid().ToString();
          //usercontext.TenantId = Guid.Parse("28C914CE-C321-4033-BB87-E9C527249503");
          usercontext.TenantId = Guid.Parse("21550810-A8E7-438F-BBF5-CB0755087356");
          usercontext.ServerName = DateTime.Now.ToLongTimeString();
      }
  </code></pre>
  
  <p>AuthorizedApiController.cs</p>
  
  <pre><code>using Agility.Core;
  using Agility.Web.Filters;
  
  namespace Agility.Web
  {
    [BusinessExceptionFilter]
    public class AuthorizedApiController : BaseApiController
    {
      public AuthorizedApiController(UserContext userContext);
  
      public UserContext UserContext { get; set; }
    }
  }
  </code></pre>