ASP.NET Core API rejects an Angular user's token: the signature key was not found
WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"Short answer
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. Compare the token's iss, ver and aud with the Authority you configured. A token from another tenant, a token version that needs the other metadata endpoint, or a pinned old key are the usual reasons.
Fix it fast
1Find out which cause you have
Decode the token header and payload. Write down kid, iss, ver and aud, then compare them with the Authority in your API.
echo "$TOKEN" | cut -d. -f1,2 | tr '.' '\n' | tr '_-' '/+' | while read p; do echo "$p==" | base64 -d 2>/dev/null; echo; done2Match what you see, then jump to its fix
- 1
The iss claim does not match the Authority in AddJwtBearer, and the kid is missing from that Authority's key listThe token was issued by a different tenant or authority than the API trustsFix - 2
The ver claim in the token is 1.0 while the Authority ends in /v2.0, or ver is 2.0 and the Authority has no /v2.0A v1.0 token is validated against the v2.0 metadata, or the other way roundFix - 3
The aud claim is not your API, or the token will not decode as a normal JWTThe Angular app sent a token meant for another resource, such as Microsoft GraphFix - 4
The code sets IssuerSigningKey, and the failures start after the identity provider rotates keysThe API pins one signing key instead of reading the key set from the authorityFix
- 1
What does the error look like?
WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"401 Unauthorized
Cause 1 of 4
The token was issued by a different tenant or authority than the API trusts
1Check
Signing keys are published per tenant. Download the key list of your Authority and look for the kid from the token header.
curl -s https://login.microsoftonline.com/<tenant-id>/discovery/v2.0/keys | grep -c "<kid-from-token>"2Fix
Point the API at the tenant that really issued the token, or make the client sign in against the tenant your API trusts. Keep one tenant-specific Authority per issuer.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://login.microsoftonline.com/<tenant-id>/v2.0";
options.Audience = "<api-application-id-uri>";
});3Verify
The grep count is 1 for the new authority, and the same request now returns 200.
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
Cause 2 of 4
A v1.0 token is validated against the v2.0 metadata, or the other way round
1Check
Read the ver claim. A v1.0 token must be validated with the keys from the v1.0 metadata endpoint, and a v2.0 token with the v2.0 one.
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null2Fix
Use the metadata endpoint that matches the token. If you control the API registration, set requestedAccessTokenVersion to 2 in the app manifest so it issues v2.0 tokens, and use the /v2.0 Authority.
// ver = 1.0 tokens
options.Authority = "https://login.microsoftonline.com/<tenant-id>";
// ver = 2.0 tokens
options.Authority = "https://login.microsoftonline.com/<tenant-id>/v2.0";3Verify
Request a new token after the change and call the API again.
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $NEW_TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
Cause 3 of 4
The Angular app sent a token meant for another resource, such as Microsoft Graph
1Check
Look at aud in the payload. Tokens for Microsoft Graph are in a proprietary format and cannot be validated by your API.
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null2Fix
Ask the identity provider for an access token whose scope belongs to your API, and attach that token to calls to your API only.
// scope of your own API, not User.Read
scopes: ['api://<api-client-id>/<scope-name>']3Verify
The new token carries your API as aud and the API returns 200.
echo "$NEW_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/nullConfirmed by learn.microsoft.com
Cause 4 of 4
The API pins one signing key instead of reading the key set from the authority
1Check
Search the project for a hard-coded signing key or certificate in TokenValidationParameters.
grep -rn "IssuerSigningKey" --include=*.cs .2Fix
Remove the pinned key and let the middleware discover keys from the Authority, so it refreshes them when the provider rolls them over.
.AddJwtBearer(options =>
{
options.Authority = "https://login.microsoftonline.com/<tenant-id>/v2.0";
options.Audience = "<api-application-id-uri>";
// no IssuerSigningKey here
});3Verify
Tokens signed with the current key are accepted after a restart.
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
How do I stop it coming back?
- Configure an Authority and let the JwtBearer middleware load keys; do not copy a public key into configuration.
- Keep one API registration per audience and request tokens with its scope from Angular.
- Log the iss, ver and aud claims of rejected tokens in development so the mismatch is visible at once.
- Do not validate or decode Microsoft Graph tokens in your own API.
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
ASP.NET Core JwtBearer throws IDX20803: Unable to obtain configuration from the metadata address
IDX20803: Unable to obtain configuration from: 'https://login.microsoftonline.com/<tenant>/v2.0/.well-known/openid-configuration'.
IDX20803 means the API could not download the OpenID Connect discovery document from Authority or MetadataAddress, so it has no signing keys.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 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 fix