.NET cannot convert the string to a JwtSecurityToken
Token handler unable to convert the token to jwt tokenShort answer
The handler only accepts a compact JWT: three dot-separated base64url segments for a signed token. Strings with a Bearer prefix, quotes, spaces, an opaque token or a token over the size limit fail. Test the string with CanReadToken to see which it is.
Fix it fast
1Find out which cause you have
Print the token's length and the number of dot-separated parts, and ask the handler whether it can read it.
var handler = new JwtSecurityTokenHandler(); Console.WriteLine($"{token.Length} chars, {token.Split('.').Length} parts, canRead={handler.CanReadToken(token)}");2Match what you see, then jump to its fix
- 1
canRead is false and the string starts with Bearer or contains quotes or whitespaceThe string includes more than the tokenFix - 2
Fewer than three parts, or the token comes from a Microsoft API such as GraphThe value is not a JWT at allFix - 3
ArgumentException on a token with a very large claims setThe token is longer than the handler's size limitFix
- 1
What does the error look like?
Token handler unable to convert the token to jwt tokenSecurityTokenMalformedException
Cause 1 of 3
The string includes more than the token
1Check
CanReadToken matches the string against a pattern of letters, digits, hyphen and underscore separated by dots. A leading scheme word, a space or a quote character fails the match.
Console.WriteLine(token.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase));2Fix
Remove the scheme and trim before reading.
var raw = authorizationHeader.Substring("Bearer ".Length).Trim().Trim('"');
var jwt = handler.ReadJwtToken(raw);3Verify
CanReadToken now returns true.
Console.WriteLine(handler.CanReadToken(raw));Confirmed by learn.microsoft.com
Cause 2 of 3
The value is not a JWT at all
1Check
Tokens that a Microsoft API receives might not always be a JWT that can be decoded; clients should treat access tokens as opaque.
Console.WriteLine(token.Split('.').Length);2Fix
Do not parse the token in the client. Send it to the API it was issued for, which validates it, and use the token response metadata for expiry and scopes.
// client: send the string as-is
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);3Verify
The API validates the token and returns data instead of a 401.
curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/meConfirmed by learn.microsoft.com
Cause 3 of 3
The token is longer than the handler's size limit
1Check
ReadJwtToken throws ArgumentException when the token length is greater than MaximumTokenSizeInBytes, and CanReadToken returns false.
Console.WriteLine($"{token.Length} vs {handler.MaximumTokenSizeInBytes}");2Fix
Issue a smaller token by putting fewer claims in it; large data belongs behind an API call.
// identity provider: reduce the claims added to the token3Verify
The read succeeds.
Console.WriteLine(handler.CanReadToken(token));Confirmed by learn.microsoft.com
How do I stop it coming back?
- Parse the Authorization header in one helper, not in each controller.
- Keep claims small; put big data behind an API call.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Security
ASP.NET Core cannot create the JWT because the signing key is too short or unusable
IDX10630: The '<key type>' for signing cannot be smaller than '<bits>' bits. KeySize: '<bits>'.
The error happens while the API signs the token, not while it checks one.
3 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
DelegateAuthenticationProvider no longer exists in the Microsoft Graph .NET SDK
The type or namespace name 'DelegateAuthenticationProvider' could not be found
Microsoft Graph .NET SDK v5 removed DelegateAuthenticationProvider.
3 causes, each with a fixSecurity
A valid JWT fails to decode because its segments are base64url, not base64
Convert.FromBase64String throws FormatException on a JWT segment
A JWT is three base64url segments joined by dots, and a standard base64 decoder refuses all three differences: base64url uses - and _ instead of + and /, leaves off the = padding, and the dots are not part of any base64 alphabet.
3 causes, each with a fixSecurity
JWT validation fails and the message only says PII is hidden
401 Unauthorized
IdentityModel hides personal data in exception text by default, so the message only says PII is hidden.
3 causes, each with a fixSecurity
JwtBearer code stops working after moving an ASP.NET Core API to .NET 8
Claims read by name no longer match after upgrading to .NET 8
Since ASP.NET Core 8, JwtBearer events return a JsonWebToken instead of a JwtSecurityToken.
3 causes, each with a fix