Sam*_*mra 3 c# jwt asp.net-web-api asp.net-core
嗨,我正在尝试使用 JWT 令牌测试一个简单的 WebApi 应用程序。主要是,我按照这里的例子
我正在使用 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(String address, CancellationToken cancel) --- 内部异常堆栈跟踪结束 --- 在 Microsoft.IdentityModel.Protocols.HttpDocumentRetriever。 GetDocumentAsync(字符串地址,CancellationToken 取消)
请注意,我在这里没有使用IdentityServer中间件,而且在浏览器上,我也无法浏览到https://localhost:44387/api/.well-known/openid-configuration
我不确定这个“openid-configuration”在哪里,特别是当我没有在我的aspnet Core 3.0 web APi 应用程序中明确使用它时?这是我的代码..
启动文件
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();
});
}
Run Code Online (Sandbox Code Playgroud)
而不是https://localhost:44387/api我也试过https://localhost:44387/和https://localhost但没有运气.. 我主要是想了解为什么 openid 在我没有使用时会出现根本。一切都在我的本地机器上,我正在使用 IISExpress。
我还尝试通过修复 IISExpress 来删除和重新创建 localhost SSL 证书。
任何线索都会有所帮助。
在您的设置中AddJwtBearer,它将联系 OIDC 元数据文档,因为您已设置Authority,用于在验证由令牌服务的私钥发布的 jwt 令牌时获取服务的公共签名密钥。
您的方案是使用对称安全密钥来发布/验证 jwt 令牌。因此,如文档所示,您不应该设置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
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6624 次 |
| 最近记录: |