IDX10503: Signature validation failed. The token's kid is: 'k2', but did not match any keys in TokenValidationParameters or Configuration.
means the token names a signing key id the API does not have, and none of the API's keys
verified the signature either. Give the API the key the token was signed with, under the
same kid, and the token is accepted.
The client sees only The signature key was not found; in a rerun on the same
lab, a token without a kid signed with another key got the same text (with
IDX10517 in the log). The kid and the keys tried are in the server console. The key ids
k1 and k2, https://issuer.test,
fix11-api, the ports and the projects api2 and mint2
are the test's own.
The error
$token = .\mint2\bin\Debug\net10.0\Mint.exe "another-test-only-key-of-fix11-9876543210" https://issuer.test fix11-api 30 kid=k2
curl.exe -i http://127.0.0.1:15601/patients -H "Authorization: Bearer $token"
HTTP/1.1 401 Unauthorized
...
WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"
The server console of the API, whose key has the id k1:
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[1]
Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10503: Signature validation failed. The token's kid is: 'k2', but did not match any keys in TokenValidationParameters or Configuration. Keys tried: 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey, KeyId: 'k1', InternalId: 'wldrSr-NT4zj4T2YRUezFgnI1w9IN6-u0IYtILAcPWU'. , KeyId: k1\r\n'.
...
Why it happens
The token maker put kid: k2 in the token header and signed with a second
made-up key; the API has one key, id k1, built from the other string. The log
lists the one key tried, and the signature did not verify with it.
The kid alone did not cause the failure in this test. A token with kid: k2
signed with the API's own key got a 200 from the same instance:
$token = .\mint2\bin\Debug\net10.0\Mint.exe "fix11-test-only-signing-key-0123456789abcdef" https://issuer.test fix11-api 30 kid=k2
curl.exe -i http://127.0.0.1:15601/patients -H "Authorization: Bearer $token"
HTTP/1.1 200 OK
...
["Maria Garcia","David Chen"]
So in this test the message appeared when both were wrong: an unknown kid and a key that does not verify. Read the kid in the log line, then find out which key carries that id on the issuing side.
The fix
The API's key and its id, from the test's Program.cs (started with --Lab:Kid=k1):
...
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!));
if (builder.Configuration["Lab:Kid"] is string kid)
{
key.KeyId = kid;
}
...
A token signed with that key and that id:
$token = .\mint2\bin\Debug\net10.0\Mint.exe "fix11-test-only-signing-key-0123456789abcdef" https://issuer.test fix11-api 30 kid=k1
curl.exe -i http://127.0.0.1:15601/patients -H "Authorization: Bearer $token"
HTTP/1.1 200 OK
...
["Maria Garcia","David Chen"]
Make the issuer and the API read the key and its id from the same configuration, so the two cannot drift apart.
How it was reproduced
A copy of the batch's first API project (made with dotnet new web -f net10.0) with Microsoft.AspNetCore.Authentication.JwtBearer
10.0.15 (Microsoft.IdentityModel 8.19.2), one endpoint protected with
RequireAuthorization(), on 127.0.0.1:15601 with its key id set to k1, ASP.NET Core
10.0.15 runtime, SDK 11.0.100. Tokens came from a copy of a console project made with dotnet new console, on
System.IdentityModel.Tokens.Jwt 8.19.2 that takes the key, issuer, audience, lifetime and an
optional kid= argument; both keys are made-up test strings. Requests went through
curl 8.22.0 on Windows.
Frequently asked
- What does IDX10503 the token's kid did not match any keys mean?
- The token's kid is not the id of any key the API has, and the keys it tried did not verify the signature. In the test the kid was k2, the API's key was k1 and the token was signed with another key.
- Does a wrong kid alone make ASP.NET Core reject a JWT?
- Not in the test. A token with kid k2 signed with the API's own key got a 200 from an API whose key id was k1.
- How do I fix IDX10503?
- Give the API the key the token was signed with, under the kid the token names. In the test a token signed with the API's key and kid k1 got a 200.
More decoded errors in the Fixes category. The same response for a token with no kid is IDX10517; a matching kid with a bad signature is IDX10511.