错误信息
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。
原因
开启 RequireHttpsMetadata 时使用了 HTTP 元数据地址
常见本地的 Keycloak 或 IdentityServer 容器通常通过 `http://service:8080` 访问。`RequireHttpsMetadata` 默认为 true,因此检索器在连接之前就会拒绝该地址(IDX20108)。
API 无法访问颁发机构
常见只在集群外部才能解析的 DNS、防火墙或出站代理,或者 `localhost` 指向容器自身的容器网络。
TLS 证书不受信任
偶见如果身份提供商使用自签名证书或内部 CA 签发的证书,那么除非在 API 容器中安装了该 CA,否则容器内的 HTTPS 请求会失败。
颁发机构路径错误
偶见缺少 `/realms/<name>`(Keycloak)、租户 ID 或 `/v2.0`(Entra ID)时,发现 URL 会返回 404。
修复方法
- 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.
在 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
相关错误
IDX10503: Signature validation failed. The token's kid is: '2026-10', but did n…Microsoft.IdentityModel.JsonWebTokens / System.IdentityModel.Tokens.Jwt (.NET)Expected 200 OK from the JSON Web Key Set HTTP responsejose (JavaScript)oidc: issuer URL provided to client ("https://accounts.example.com") did not ma…coreos/go-oidc (Go)