Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

ASP.NET Core API rejects an Angular user's token: the signature key was not found

ErrorWWW-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.

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

Fix it fast

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

    1. 1The 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. 2The 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. 3The 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. 4The 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

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

Confirmed by learn.microsoft.com

Did this fix it?

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

2Fix

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

Confirmed by learn.microsoft.com

Did this fix it?

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

2Fix

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

Confirmed by learn.microsoft.com

Did this fix it?

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

Confirmed by learn.microsoft.com

Did this fix it?

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

Next step

Want this handled for good?

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