title: How to read Claims from JWT Token .NET 4.5
body: <p>I can't read token claims from Bearer JWT token. Login is working, the http request comes with a valid jwt token to the backend.
  The application is self hosted on IIS7.
  Here is my code on server side:</p>
  
  <p><strong>SecurityConfig.cs</strong></p>
  
  <pre><code>app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
  {
      TokenEndpointPath = new PathString("/token"),
      AccessTokenExpireTimeSpan = TimeSpan.FromHours(24),
      Provider = new AuthorizationServerProvider() ,
      AccessTokenFormat = new JwtFormat(TimeSpan.FromHours(24))
  });
  
  app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
  </code></pre>
  
  <p><strong>AuthorizationServerProvider.cs</strong></p>
  
  <pre><code>ClaimsIdentity id = new ClaimsIdentity(context.Options.AuthenticationType);
  
  id.AddClaim(new Claim(InosysClaimTypes.UserId, Convert.ToString(appContext.UserId)));
  
  id.AddClaim(new Claim(InosysClaimTypes.Username, context.UserName));
  id.AddClaim(new Claim(InosysClaimTypes.Password, context.Password));
  
  id.AddClaim(new Claim(InosysClaimTypes.FirNr, Convert.ToString(appContext.FirmenNummer)));
  id.AddClaim(new Claim(InosysClaimTypes.FirNdl, Convert.ToString(appContext.Niederlassung)));
  id.AddClaim(new Claim(InosysClaimTypes.Bereich, Convert.ToString(appContext.Bereich)));
  
  id.AddClaim(new Claim(InosysClaimTypes.Sprache, Convert.ToString(appContext.Sprache)));
  id.AddClaim(new Claim(InosysClaimTypes.SchiffNummern, appContext.SchiffNummern == null ? "" : string.Join(",", appContext.SchiffNummern)));
  id.AddClaim(new Claim(InosysClaimTypes.Geschaeftsjahr, Convert.ToString(appContext.Geschaeftsjahr)));
  
  var principal = new ClaimsPrincipal(id);
  Thread.CurrentPrincipal = principal;
  if (HttpContext.Current != null)
  {
      HttpContext.Current.User = principal;
  }
  
  context.Validated(id);
  </code></pre>
  
  <p>In the ApiController i try to get the caller's payload information like this:</p>
  
  <pre><code>ClaimsIdentity identity = User.Identity as ClaimsIdentity;
  
  if (identity != null)
  {
      appContext.UserId = Convert.ToInt32(identity.FindFirst(InosysClaimTypes.UserId).Value);
      appContext.Username = identity.FindFirst(InosysClaimTypes.Username).Value;
  }
  </code></pre>
  
  <p>That is the identity variable debugged:
  <a href="https://i.stack.imgur.com/QO9tF.png" rel="nofollow noreferrer">identity</a></p>