Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

JWT validation fails and the message only says PII is hidden

Error[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.

  • Applies to ASP.NET Core 8, 9 and 10, Microsoft.IdentityModel 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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;
    }
  2. 2Match what you see, then jump to its fix

    1. 1Messages contain the PII hidden placeholder where an issuer, audience, key or token should beShowPII is off, so exception values are replacedFix
    2. 2The API returns 401 and the log has no line explaining whyThe failure reason is never written anywhereFix
    3. 3ShowPII is on and the log still does not contain the complete tokenYou need the whole token in the log, not just the valuesFix

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/values

Confirmed by github.com

Did this fix it?

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 run

Confirmed by learn.microsoft.com

Did this fix it?

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 run

Confirmed by github.com

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.