0

I want to inspect a jwt token just to peak on the content. I don't have the private key to validate it, but I still want to explore the content. When I try using System.IdentityModel.Tokens.Jwt I don't get the claims as expected. I can see the token and payload on jwt.io, but not when I try in code. The code I have use is this:

var handler = new JwtSecurityTokenHandler();
var jsonToken = handler.ReadJwtToken(token);

Console.WriteLine("Claims: " + jsonToken.Claims.Count());
jsonToken.Claims.ToList().ForEach(c => Console.WriteLine("==> C: " + c.ToString()));

For the token I have it return 0 claims. I would expect to be able to access the payload of the token the same way as I see it on jwt.io, but can't figure out how. What am I doing wrong.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137

1 Answers1

0

So inspect it I just decided to take the payload part and generate the json from it.

            var payload = token.Split(".")[1];
            var paddedToken = payload.PadRight(payload.Length + (4 - payload.Length % 4) % 4, '=');
            var bytes = Convert.FromBase64String(paddedToken);
            var jsonPayload = System.Text.Encoding.UTF8.GetString(bytes);
            var json = System.Text.Json.JsonDocument.Parse(jsonPayload);
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137