JWT validation fails and the message only says PII is hidden
[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]Short answer
IdentityModel hides personal data in exception text by default, so the message only says PII is hidden. Set IdentityModelEventSource.ShowPII to true in development to see the real values, then log the failure from the JwtBearerEvents callbacks. Turn ShowPII off again before production.
Fix it fast
1Find out which cause you have
Turn on PII display for the development environment only, restart the API and repeat the failing request.
// Program.cs if (app.Environment.IsDevelopment()) { Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; }2Match what you see, then jump to its fix
- 1
Messages contain the PII hidden placeholder where an issuer, audience, key or token should beShowPII is off, so exception values are replacedFix - 2
The API returns 401 and the log has no line explaining whyThe failure reason is never written anywhereFix - 3
ShowPII is on and the log still does not contain the complete tokenYou need the whole token in the log, not just the valuesFix
- 1
What does the error look like?
[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]401 Unauthorized
Cause 1 of 3
ShowPII is off, so exception values are replaced
1Check
The IdentityModel project keeps potential PII out of exceptions by default to comply with GDPR, and the placeholder is what appears instead.
grep -rn "ShowPII" --include=*.cs .2Fix
Set ShowPII to true while debugging, read the real value in the message, fix the configuration, and remove the setting or guard it with a development check.
if (app.Environment.IsDevelopment())
{
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
}3Verify
Repeat the failing request. The message now shows the actual issuer, audience or key text.
curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by github.com
Cause 2 of 3
The failure reason is never written anywhere
1Check
Microsoft's troubleshooting article uses the JwtBearerEvents callbacks. OnAuthenticationFailed runs when the token fails validation, and OnChallenge runs last, before the 401 is sent.
grep -rn "JwtBearerEvents" --include=*.cs .2Fix
Log the exception in OnAuthenticationFailed while you debug.
builder.Services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = ctx =>
{
Console.WriteLine(ctx.Exception.ToString());
return Task.CompletedTask;
}
};
});3Verify
The console shows the exception text for the next failing request.
dotnet runConfirmed by learn.microsoft.com
Cause 3 of 3
You need the whole token in the log, not just the values
1Check
By default the logger does not record complete tokens, even with ShowPII on.
grep -rn "LogCompleteSecurityArtifact" --include=*.cs .2Fix
Enable full logging of the security artifact in a development run only, because the log then holds a usable credential.
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
Microsoft.IdentityModel.Logging.IdentityModelEventSource.LogCompleteSecurityArtifact = true;3Verify
The log line now contains the full token. Delete the logs when done.
dotnet runConfirmed by github.com
How do I stop it coming back?
- Wrap ShowPII in an IsDevelopment check so it cannot ship on.
- Never keep logs that contain full tokens.
- Log validation failures through JwtBearerEvents so a 401 always leaves a reason.
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
.NET cannot convert the string to a JwtSecurityToken
SecurityTokenMalformedException
The handler only accepts a compact JWT: three dot-separated base64url segments for a signed token.
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
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