Skip to main content

How to Read a JWT Without Validating It in C# with ReadJsonWebToken

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

JsonWebTokenHandler.ReadJsonWebToken turns a JWT string into a JsonWebToken object you can read: its algorithm, its key ID, its subject, its expiry and any other claim. It needs no key and checks nothing, which makes it right for reading a token and wrong for trusting one.

Syntax​

Syntax
using Microsoft.IdentityModel.JsonWebTokens;

JsonWebToken <jwt> = new JsonWebTokenHandler().ReadJsonWebToken(<token>);
PartWhat it does
ReadJsonWebToken(<token>)Parses the token's header and payload without validating anything.
<jwt>.Alg, <jwt>.KidValues from the header: the algorithm and the key ID.
<jwt>.Subject, <jwt>.ValidToValues from the payload: sub, and exp as a UTC DateTime.

Microsoft Learn on ReadJsonWebToken: "Converts a string into an instance of JsonWebToken." And in its remarks: "The token is NOT validated and no security decisions should be made about the contents. Use ValidateToken(String, TokenValidationParameters) or ValidateTokenAsync(String, TokenValidationParameters) to ensure the token is acceptable."

The Example​

The example reads a token that another service signed with a key this program doesn't have. The token expired on 1 December 2025, and its header names the key that signed it, key-2025-12, in a kid.

Prerequisites​

  • .NET SDK. This guide uses .NET SDK 10.0.404.

Step 1: Create the Project​

Terminal
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:

Program.cs, which holds a token from another service that expired in December 2025, reads it with ReadJsonWebToken, and writes its Alg, Kid, Subject and ValidTo.

Program.cs
using Microsoft.IdentityModel.JsonWebTokens;

// A token from another service, signed with a key this program doesn't have.
// It expired on 1 December 2025.
var token = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE";

var jwt = new JsonWebTokenHandler().ReadJsonWebToken(token);
Console.WriteLine($"alg: {jwt.Alg}, kid: {jwt.Kid}");
Console.WriteLine($"subject: {jwt.Subject}");
Console.WriteLine($"valid to: {jwt.ValidTo:O}");

Step 3: Run It​

Terminal
dotnet run
Output
alg: HS256, kid: key-2025-12
subject: ada
valid to: 2025-12-01T01:00:00.0000000Z

A zsh terminal where dotnet run prints alg HS256 and kid key-2025-12, subject ada, and valid to 2025-12-01 at 01:00 UTC.

The command is the same in zsh, bash and PowerShell.

ReadJsonWebToken read every value without the key, and didn't mind that ValidTo lies in the past.

Reading Is Not Validating​

Anyone can write a token with any claims, so what ReadJsonWebToken returns proves nothing. Use it to look, never to decide who someone is or what they may do. To trust a token, validate it:

Program.cs
var result = await new JsonWebTokenHandler().ValidateTokenAsync(token, parameters);

ValidateTokenAsync checks the signature with the key in parameters, the algorithm and the lifetime, and result.IsValid says whether the token passed.

Reading the header first is the one common server-side use: a service with several keys reads Kid and picks that key from its own list. The header is just as forgeable as the claims, so look it up only among keys you already trust.

Common Mistakes​

MistakeWhat happensDo this instead
Making decisions from the claims of a token you only readAnyone can send a token with any claimsValidate with ValidateTokenAsync first
Expecting ReadJsonWebToken to reject an expired tokenIt read a token whose ValidTo had passed, without an errorValidate; the lifetime check is on by default
Passing the whole Authorization header valueArgumentException: IDX14102: Unable to decode the header ...Remove Bearer first

Troubleshooting​

SymptomLikely causeFix
SecurityTokenMalformedException: IDX14100: JWT is not well formed, there are no dots (.).The string isn't a JWTCheck what you passed
[PII of type '...' is hidden] in a messageIdentityModel hides personal data in its messages by defaultRead the message without the hidden part; it still names the problem

FAQ​

What's the difference between ReadJsonWebToken and ReadToken?​

ReadToken returns the general SecurityToken type, while ReadJsonWebToken returns JsonWebToken, whose properties you can read without a cast.

How do I read a claim that has no property, such as role?​

Call GetPayloadValue<string>("role") on the JsonWebToken.

Conclusion​

new JsonWebTokenHandler().ReadJsonWebToken(token) reads a token's header and claims without a key and without any checks. Use it to look inside a token; validate with ValidateTokenAsync before you trust it.

Additional Resources​