跳到主要内容
ecdsa.com
Microsoft.IdentityModel.JsonWebTokens / System.IdentityModel.Tokens.Jwt (.NET).NET

错误信息

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"`。

原因

修复方法

  1. 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. 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;
    }

相关错误

浏览完整的签名错误库