跳到主要内容
ecdsa.com
Microsoft.IdentityModel.Protocols (.NET).NET

错误信息

IDX20803: Unable to obtain configuration from: 'http://keycloak:8080/realms/main/.well-known/openid-configuration'.

也可能输出为

  • System.InvalidOperationException: IDX20803: Unable to obtain configuration from:
  • IDX20108: The address specified 'http://keycloak:8080/realms/main/.well-known/openid-configuration' is not valid as per HTTPS scheme. Please specify an https address for security reasons.
  • IDX20804: Unable to retrieve document from:

消息后面接着是“Will retry at '…'. Exception: '…'”以及内部异常的文本。其中的 URL 是你的 `Authority` + `/.well-known/openid-configuration`,或者你的 `MetadataAddress`。

Microsoft.IdentityModel.Protocols (.NET) — ConfigurationManager<OpenIdConnectConfiguration>, used by JwtBearer and OpenIdConnect handlers

含义

配置管理器无法下载(或解析)OpenID Connect 发现文档,因此 JWT Bearer 或 OpenID Connect 处理程序既没有颁发者,也没有签名密钥。随后每个令牌都会被拒绝,通常紧接着出现 IDX10500/IDX10503 错误。真正的原因在内部异常中:如果要求 HTTPS 而元数据地址是明文 HTTP,则为 IDX20108;否则为带有网络或 TLS 错误的 IDX20804。

原因

修复方法

  1. 1.

    只在开发环境中允许 HTTP 元数据

    把 `RequireHttpsMetadata = false` 限定在开发环境中;在生产环境中,颁发机构必须使用 HTTPS,因为你下载的密钥决定了你会接受哪些令牌。

    csharp
    builder.Services.AddAuthentication().AddJwtBearer(options =>
    {
        options.Authority = builder.Configuration["Auth:Authority"];
        // Plain HTTP only for local development containers; production authorities must be HTTPS.
        options.RequireHttpsMetadata = !builder.Environment.IsDevelopment();
    });
  2. 2.

    在 API 运行的位置请求该 URL

    在 API 所在的容器或 Pod 中发出请求。返回 200 和 JSON 即可确认网络和 TLS 正常;其他任何结果都会指出需要修复的是哪一层。

    bash
    # From inside the API container: DNS, TLS and HTTP status of the metadata URL
    curl -sS -o /dev/null -w '%{http_code} %{ssl_verify_result}\n' https://login.example.com/realms/main/.well-known/openid-configuration

相关错误

浏览完整的签名错误库