title: Get JWT claims directly from the token, ASP Net Core 2.1
body: <p>I working on an ASP Net Core 2.1 Web API. I've implemented successfully JWT within my project. Everything with the Authorization works fine.</p>
  <p>Normally, when I need user claims, I know I can get them like this (E.g. Email claim):</p>
  <pre class="lang-cs prettyprint-override"><code>var claimsIdentity = User.Identity as ClaimsIdentity;
  var emailClaim = claimsIdentity.Claims.FirstOrDefault(x =&gt; x.Type == ClaimTypes.Email);
  </code></pre>
  <p>The thing is, I am not in a controller that inherits from <code>ControllerBase</code> class, so I don't have any <code>User</code> object or <code>[Authorize]</code> attributes.</p>
  <p>What I have though is the token itself.<br />
  e.g.</p>
  <pre><code>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsIm5iZiI6MTU2ODYzNjYxMywiZXhwIjoxNTY4NjQ3NDEzLCJpYXQiOjE1Njg2MzY2MTN9.ED9x_AOvkLQqutb09yh3Huyv0ygHp_i3Eli8WG2S9N4
  </code></pre>
  <p>I want to get the claims directly from the token, because:</p>
  <ol>
  <li>I have access to the token.</li>
  <li>I am not located in a Controller class and the request is not going through any <code>[Authorize]</code> attributes, so <code>IHttpContextAccessor</code> can't be used as well.</li>
  </ol>
  <p>How can I achieve this in ASP Net Core 2.1? In case someone wants to see how I add the user claims:</p>
  <pre class="lang-cs prettyprint-override"><code>var tokenDescriptor = new SecurityTokenDescriptor
  {
      Expires = DateTime.UtcNow.AddHours(3),
      Subject = new ClaimsIdentity(new[]
      {
          new Claim(ClaimTypes.Name, email),
          new Claim(ClaimTypes.Email, email)
      }),
      SigningCredentials = new SigningCredentials(key: new SymmetricSecurityKey(key), algorithm: SecurityAlgorithms.HmacSha256Signature)
  };
  
  var token = tokenHandler.CreateToken(tokenDescriptor);
  </code></pre>
  <p>I'm located in a class that derives from <code>IDocumentFilter</code> (Swagger class)</p>