【问题标题】:IDX:20803 Unable to obtain configuration from... - Web APIIDX:20803 无法从...获取配置 - Web API
【发布时间】:2025-12-30 01:50:07
【问题描述】:

您好,我正在尝试使用 JWT 令牌测试一个简单的 WebApi 应用程序。主要是我按照例子here

我正在使用 https。

暂时不使用授权。问题是,如果我在 Postman 中选择“从父​​级继承身份验证”,一切正常。

只要我将选项更改为“BearerToken”并尝试输入我收到的 JSONWebToken,它就会给我

System.InvalidOperationException:IDX20803:无法从“https://localhost:44387/api/.well-known/openid-configuration”获取配置。 ---> System.IO.IOException:IDX20804:无法从“https://localhost:44387/api/.well-known/openid-configuration”检索文档。 ---> System.Net.Http.HttpRequestException:响应状态码不表示成功:404(未找到)。 在 System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() 在 Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(字符串地址,CancellationToken 取消) --- 内部异常堆栈跟踪结束 --- 在 Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(字符串地址,CancellationToken 取消)

请注意,我在这里没有使用 IdentityServer 中间件,而且在浏览器上,我也无法浏览到 https://localhost:44387/api/.well-known/openid-configuration

我不确定这个 "openid-configuration" 在哪里,特别是当我没有在我的 aspnet Core 3.0 Web APi 应用程序中明确使用它时?这是我的代码..

Startup.cs

public void ConfigureServices(IServiceCollection services)
    { 
     //extra code removed for brevity
     //Authentication
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority = "https://localhost:44387/api";
                options.Audience = "JWT:Issuer";
                options.TokenValidationParameters.ValidateLifetime = true;
                options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(5);
                options.RequireHttpsMetadata = false;

            });
 }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
       if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            //to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.
            app.UseHsts();
        }

        //to redirect HTTP requests to HTTPS.
        app.UseHttpsRedirection();

        app.UseAuthentication();

        app.UseRouting();





        app.UseEndpoints(endpoints =>
        {

            endpoints.MapControllers();

            endpoints.MapRazorPages();
        });
    }

而不是 https://localhost:44387/api 我也尝试过 https://localhost:44387/https://localhost 但没有运气.. 我主要是想了解为什么 openid 在我根本没有使用时会出现。一切都在我的本地机器上,我正在使用 IISExpress。

我还尝试通过修复 IISExpress 删除和重新创建 localhost SSL 证书。

任何线索都会有所帮助。

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api jwt


    【解决方案1】:

    在您的AddJwtBearer设置中,由于您设置了Authority,它将联系OIDC元数据文档,用于在验证由令牌服务的私钥颁发的jwt令牌时获取服务的公共签名密钥。

    您的方案是使用对称安全密钥来发布/验证 jwt 令牌。所以正如document 所示,您应该 设置Authority,设置SymmetricSecurityKey 以在场景中验证令牌:

    var appSettingsSection = Configuration.GetSection("AppSettings");
    services.Configure<AppSettings>(appSettingsSection);
    
    // configure jwt authentication
    var appSettings = appSettingsSection.Get<AppSettings>();
    var key = Encoding.ASCII.GetBytes(appSettings.Secret);
    services.AddAuthentication(x =>
    {
        x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(x =>
    {
        x.RequireHttpsMetadata = false;
        x.SaveToken = true;
        x.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = new SymmetricSecurityKey(key),
            ValidateIssuer = false,
            ValidateAudience = false
        };
    });
    

    【讨论】:

    • 太棒了!所以有两件事我不知道,1.权威基本上是为签名提供公钥..2。因为我的启动代码已经写好了,所以我完全忽略了示例文档中的那个部分:D...谢谢你的指点!!
    • 这个答案帮助我对前端和后端的身份验证中间件进行了一些有用的更改。首先我得到 401,然后它开始给出 500,在检查前端时我意识到令牌在发送到 api 时总是未定义,导致 500 错误身份验证