How to Read a Claim from a JsonWebToken in C# with GetPayloadValue
A JsonWebToken has properties for a few standard claims, such as Subject, but your own claims, such as role, you read by name with GetPayloadValue<T>. It returns the value as the type you ask for, and throws when the claim is missing or has another type. TryGetPayloadValue<T> returns false instead.
Syntax
T value = jwt.GetPayloadValue<T>("<claim>");
bool found = jwt.TryGetPayloadValue<T>("<claim>", out T value);
| Part | What it does |
|---|---|
T | The type you want the claim as, such as string or long. |
"<claim>" | The claim's name in the payload, such as "role". |
GetPayloadValue<T> | Returns the claim as T. Throws ArgumentException when it can't. |
TryGetPayloadValue<T> | Returns true and the value when the claim is there and fits T, false otherwise. |
Microsoft Learn on GetPayloadValue: it "Gets the 'value' corresponding to key from the JWT payload transformed as type 'T'." It throws an ArgumentException, "Thrown if claim is not found or a transformation to T cannot be made."
The Example
The example reads a token from another service whose claims are sub, role, iat and exp. It reads role as a string and exp as a long, tries an email claim the token doesn't have with TryGetPayloadValue, and then asks for email with GetPayloadValue.
Prerequisites
- .NET SDK. This guide uses .NET SDK 10.0.404.
Step 1: Create the Project
dotnet new console -o demo
cd demo
dotnet package add Microsoft.IdentityModel.JsonWebTokens
dotnet new console creates a console app in a new demo folder, and dotnet package add adds Microsoft.IdentityModel.JsonWebTokens, version 8.23.0 here.
Step 2: Write the Code
Replace the contents of Program.cs:

using Microsoft.IdentityModel.JsonWebTokens;
// A token from another service. Its claims are sub, role, iat and exp.
var token = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE";
var jwt = new JsonWebTokenHandler().ReadJsonWebToken(token);
Console.WriteLine(jwt.GetPayloadValue<string>("role"));
Console.WriteLine(jwt.GetPayloadValue<long>("exp"));
Console.WriteLine(jwt.TryGetPayloadValue<string>("email", out var email) ? email : "no email claim");
Console.WriteLine(jwt.GetPayloadValue<string>("email"));
ReadJsonWebToken reads the token without validating it, which is fine for this example. In an application, read claims from a token you've validated.
Step 3: Run It
dotnet run
reader
1764550800
no email claim
Unhandled exception. System.ArgumentException: IDX14304: Claim with name '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' does not exist in the JsonClaimSet.
at Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet.GetValue[T](String key, Boolean throwEx, Boolean& found)
at Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet.GetValue[T](String key)
at Microsoft.IdentityModel.JsonWebTokens.JsonWebToken.GetPayloadValue[T](String key)
at Program.<Main>$(String[] args) in /Users/Shared/you/demo/Program.cs:line 10

The command is the same in zsh, bash and PowerShell.
What Happened
GetPayloadValue<string>("role")returnedreader, andGetPayloadValue<long>("exp")returned the expiry as a number of seconds.TryGetPayloadValue<string>("email", ...)returnedfalse, so the code printed its fallback.GetPayloadValue<string>("email")threwIDX14304because the claim isn't there. The claim's name appears as[PII of type 'System.String' is hidden ...]: IdentityModel keeps values out of its messages unless you allow it, in case they're personal data.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling GetPayloadValue for a claim that may be missing | ArgumentException: IDX14304: Claim with name ... does not exist in the JsonClaimSet. | Use TryGetPayloadValue |
Asking for the wrong type, as in GetPayloadValue<int>("role") | ArgumentException: IDX14305: Unable to convert the 'role' json property to the following type: 'System.Int32'. Property type was: 'System.String'. | Ask for the type the JSON holds |
Reading exp as int | Unix times fit in int only until 2038 | Use long, or the ValidTo property |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
IDX14304 | The claim isn't in the token, or its name differs in case | Check the name against the token's payload |
IDX14305 | The claim exists with another JSON type | Change T to match |
FAQ
Why is the claim name hidden in the error?
IdentityModel hides values it treats as personal data in its exception messages by default. Its message links to https://aka.ms/IdentityModel/PII, which explains the setting.
Is there a property for the standard claims?
For some: Subject reads sub, Issuer reads iss, and ValidTo reads exp as a DateTime.
Conclusion
jwt.GetPayloadValue<T>("claim") reads a claim as the type you need and throws when it can't; jwt.TryGetPayloadValue<T>("claim", out var value) returns false instead. Read claims only from a token you've validated when they decide anything.
Additional Resources
- JsonWebToken.GetPayloadValue and JsonWebToken.TryGetPayloadValue, on Microsoft Learn.
