【发布时间】: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