ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
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).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.
Fix it fast
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/values2Match what you see, then jump to its fix
- 1
The 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
The header is Bearer null, Bearer undefined or just BearerThe token is empty, null or undefined when the request leaves the browserFix - 3
The 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
- 1
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/values2Fix
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 -cConfirmed by rfc-editor.org
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 -c2Fix
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/valuesConfirmed by rfc-editor.org
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 -c2Fix
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 Authorization3Verify
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/valuesConfirmed by learn.microsoft.com
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
Security
An ASP.NET Core API answers 401 with Bearer error="invalid_token"
401 Unauthorized
invalid_token in the WWW-Authenticate header means the access token is expired, revoked, malformed or invalid for another reason.
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
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fix