错误信息
IDX10503: Signature validation failed. The token's kid is: '2026-10', but did not match any keys in TokenValidationParameters or Configuration.
也可能输出为
- IDX10503: Signature validation failed. Token does not have a kid. Keys tried:
- IDX10517: Signature validation failed. The token's kid is missing.
- Bearer error="invalid_token", error_description="The signature key was not found"
完整文本后面还有“Keys tried: …”、密钥数量以及 `[PII of type … is hidden]` 占位符(在开发环境中设置 `IdentityModelEventSource.ShowPII = true` 即可看到这些值)。在 6.36.0、7.7.1 和 8.23.0 上测试过:6.x 的措辞是“IDX10503: Signature validation failed. Token does not have a kid. Keys tried: …”;7.x 和 8.x 使用上面的措辞,并把不带 `kid` 的令牌报告为 IDX10517“The token's kid is missing”。
Microsoft.IdentityModel.JsonWebTokens / System.IdentityModel.Tokens.Jwt (.NET) — SecurityTokenSignatureKeyNotFoundException
含义
令牌用 `kid` 指明了签名密钥,但无论是 `TokenValidationParameters`(`IssuerSigningKey(s)`)中,还是从颁发机构的 JWKS 下载的配置中,都没有具有该密钥 ID 的密钥。根本没有进行签名计算——找不到候选密钥。在 ASP.NET Core 中,客户端看到的是 `WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"`。
原因
颁发者轮换了签名密钥
常见静态配置的密钥(或从未刷新的缓存配置)只知道之前的 `kid`。Entra ID、Auth0、Keycloak 和 IdentityServer 都会轮换密钥。
颁发机构或元数据地址属于另一个租户
常见Entra ID 的 v1 与 v2 端点、不同的租户 ID、各自带有独立元数据的 B2C 策略,或者预发布环境的颁发机构。下载到的密钥是有效的——只不过是用于别人的令牌。
无法加载配置
偶见元数据下载失败时,处理程序会在配置密钥数量为零的情况下验证令牌(“Number of keys in Configuration: '0'”)。真正的错误(IDX20803)会单独记录在日志中。
颁发者有多个密钥,却只配置了一个 IssuerSigningKey
偶见把 JWKS 中的一个密钥硬编码下来,在颁发者改用密钥集中的另一个密钥签名之前都能正常工作。
修复方法
- 1.
让处理程序从颁发机构获取密钥
设置了 `Authority` 后,`JwtBearer` 会通过 OpenID Connect 发现机制下载颁发者的 JWKS,定期刷新,并且在令牌指明了未知密钥时也会刷新(`RefreshOnIssuerKeyNotFound`,默认开启)。
csharp builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { // Keys come from {Authority}/.well-known/openid-configuration -> jwks_uri and are refreshed automatically. options.Authority = "https://login.example.com/"; options.Audience = "api://orders"; }); - 2.
调试时显示被隐藏的值
IdentityModel 默认会在异常文本中隐藏密钥 ID 和令牌内容。只在开发环境中启用 `ShowPII`,以查看尝试过哪些密钥。
csharp // Development only: show the hidden values ([PII of type ... is hidden]) in IDX messages. if (builder.Environment.IsDevelopment()) { Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; }
相关错误
IDX10511: Signature validation failed. Keys tried:Microsoft.IdentityModel.JsonWebTokens / System.IdentityModel.Tokens.Jwt (.NET)IDX20803: Unable to obtain configuration from: 'http://keycloak:8080/realms/mai…Microsoft.IdentityModel.Protocols (.NET)no applicable key found in the JSON Web Key Setjose (JavaScript)Unable to find a signing key that matches '2026-10-rotated'jwks-rsa (Node.js)