Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

ASP.NET Core rejects the token before checking it: IDX12741, three segments expected

ErrorIDX12741: JWT: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' must have three segments (JWS) or five segments (JWE).

Short answer

IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five. Log the raw header the API receives. Usually it holds a doubled Bearer word, an empty or undefined value, or a token of the wrong kind.

  • Applies to ASP.NET Core 8, 9 and 10, Microsoft.AspNetCore.Authentication.JwtBearer, Angular 19, 20
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the same request with curl and a token you trust. If it works, the fault is in what Angular sends, so compare the two Authorization headers character by character.

    curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/values
  2. 2Match what you see, then jump to its fix

    1. 1The failing request shows Authorization: Bearer Bearer ... or quote marks around the token in the browser network tabThe Authorization header holds the word Bearer twice, or extra characters around the tokenFix
    2. 2The header is Bearer null, Bearer undefined or just BearerThe token is empty, null or undefined when the request leaves the browserFix
    3. 3The decoded value does not split into three parts, and it was returned by the refresh call or a different token fieldThe value is a different kind of token, such as a refresh token or an opaque tokenFix

What does the error look like?

  • IDX12741: JWT: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' must have three segments (JWS) or five segments (JWE).
  • 401 Unauthorized

Cause 1 of 3

The Authorization header holds the word Bearer twice, or extra characters around the token

1Check

The header format is the scheme name, one or more spaces, then the token. A token may only use letters, digits and - . _ ~ + / with optional = at the end, so a second word or a quote mark makes it invalid. Open the request in the browser's network tab and read the header.

curl -i -H "Authorization: Bearer Bearer $TOKEN" https://localhost:5001/api/values

2Fix

Add the prefix in one place only, and send the raw token string, not JSON text.

// auth.interceptor.ts
export const authInterceptor: HttpInterceptorFn = (req, next) => {
  const token = inject(AuthService).accessToken(); // raw token, no prefix
  return token
    ? next(req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }))
    : next(req);
};

3Verify

The header in the network tab reads Bearer followed by one token with two dots.

echo "$TOKEN" | tr -cd '.' | wc -c

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 3

The token is empty, null or undefined when the request leaves the browser

1Check

A string such as undefined has no dots at all, so it cannot be a JWT. Log the value your interceptor reads, especially on the first request after a page reload or after sign-out.

echo "undefined" | tr -cd '.' | wc -c

2Fix

Attach the header only when a real token exists, and request a fresh one when it does not.

const token = inject(AuthService).accessToken();
if (!token) {
  return next(req); // let the API answer 401 cleanly, then sign in again
}
return next(req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }));

3Verify

The first request after reload carries either no header or a three-part token.

curl -i https://localhost:5001/api/values

Confirmed by rfc-editor.org

Did this fix it?

Cause 3 of 3

The value is a different kind of token, such as a refresh token or an opaque token

1Check

Count the dots in the exact string you send. Access tokens can have any format, and a refresh token is a separate kind of token, used to obtain new tokens. Check which field of the token response your code stores.

echo "$VALUE_SENT" | tr -cd '.' | wc -c

2Fix

Send the access token the identity provider issued for your API. Keep the refresh token for the token endpoint only.

// store separately
this.accessToken = response.access_token;
this.refreshToken = response.refresh_token; // never put this in Authorization

3Verify

The string sent to the API has two dots and the API returns 200.

curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $ACCESS_TOKEN" https://localhost:5001/api/values

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Build the Authorization header in one interceptor and nowhere else.
  • Keep access token and refresh token in separately named fields.
  • Skip the header, instead of sending a placeholder, when there is no token.
  • Turn on PII display in development only, so the exception shows the rejected value.

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.

Related fixes

Next step

Want this handled for good?

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