请帮助我了解 ASP netcore 应用程序和 netcore Kestrel 托管应用程序的 JWT 令牌验证之间的区别。
有两个应用程序使用源代码验证令牌,如下所示:
public static IServiceCollection AddJwtToken(this IServiceCollection services, OAuthConfig config)
{
services.AddMvc();
services.AddAuthorization();
Logger.DebugFormat("AddJwtBearer authority:{0} audience:{1}", config.GetAuthority(), config.Resource);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => new JwtBearerOptions
{
Authority = config.GetAuthority(),
Audience = config.Resource,
});
return services;
}
它非常简单,并且如果从 asp net core 2.2 应用程序验证令牌,则效果很好
// in the asp.net core
var builder = WebHost.CreateDefaultBuilder(args);
builder
.UseStartup<Startup>()
.ConfigureKestrel(_ => _.ConfigureEndpoints())
.UseSerilog();
还有另一个应用程序(控制台)使用以下命令启动相同的休息服务主机UseKestrel
//in the console app
var builder = WebHost.CreateDefaultBuilder()
.UseNLog()
.UseKestrel(_ => _.ConfigureEndpoints())
.UseStartup<Startup>();
唯一的一个显着区别是UseKestrel
在控制台中通过ConfigureKestrel
对于 ASP.NET 核心。
使用相同的源代码(和配置)从 Azure AD 获取令牌。
请找到它作为这里的要点 https://gist.github.com/oleksabor/a4c873e078f74086ddda57b6b29f2c74。
它被配置为从https://login.microsoftonline.com/{tenant}/v2.0
提供者。两种情况使用相同的令牌端点、clientid、秘密和范围值。
问题是AddJwtBearer
在 ASP.NET Core 中验证令牌,但不在控制台应用程序中验证令牌。
错误是
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10501: Signature validation failed. Unable to match keys:
kid: 'BB8CeFVqyaGrGNuehJIiL4dfjzw',
token: '{"typ":"JWT","alg":"RS256","kid":"BB8CeFVqyaGrGNuehJIiL4dfjzw"}.{"aud":"2c163c99-935b-4362-ae0d-657f589f5565","iss":"https://login.microsoftonline.com/{tenantidhere}/v2.0
为什么asp.net core主机验证令牌(对于第一个AddJwtBearer
实现)和控制台主机失败?
谢谢