gpt4 book ai didi

c# - IDX10501 : Signature validation failed. 无法匹配 key

转载 作者:行者123 更新时间:2023-12-02 00:03:22 24 4
gpt4 key购买 nike

请帮助我了解 ASP netcore 应用程序和 netcore Kestrel 托管应用程序中的 JWT token 验证之间的区别。

有两个应用程序使用如下源代码验证 token :

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 应用程序验证 token ,它会很好地工作

// 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>();

唯一的一个显着区别是,对于 ASP.NET Core,通过 ConfigureKestrel 在控制台中有 UseKestrel

相同的源代码(和配置)用于从 Azure AD 获取 token 。请查找为the gist here 。它配置为从 https://login.microsoftonline.com/{tenant}/v2.0 提供商获取 token 。这两种情况使用相同的 token 端点、clientid、 secret 和范围值。

问题是 AddJwtBearer 在 asp.net core 中验证 token ,但不在控制台应用程序中验证 token 。错误是

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 主机验证 token (对于第一个 AddJwtBearer 实现)而控制台主机失败?

谢谢

最佳答案

要解决此错误,我必须从 openid 提供程序加载 key ,如下所示:

Logger.DebugFormat("AddJwtBearer authority:{0} audience:{1}", config.GetAuthority(), config.Resource);

IList<string> validissuers = new List<string>()
{
config.GetAuthority(),
};

var configManager = new ConfigurationManager<OpenIdConnectConfiguration>($"{validissuers.Last()}/.well-known/openid-configuration", new OpenIdConnectConfigurationRetriever());

var openidconfig = configManager.GetConfigurationAsync().Result;

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, _ =>
{
_.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateAudience = true,
ValidAudience = config.Resource,

ValidateIssuer = true,
ValidIssuers = new[] { config.GetAuthority() },

ValidateIssuerSigningKey = true,
IssuerSigningKeys = openidconfig.SigningKeys,

RequireExpirationTime = true,
ValidateLifetime = true,
RequireSignedTokens = true,
};

_.RequireHttpsMetadata = false;

});

它开始适用于这两种情况。但是旧的 AddJwtBearer 实现和新的实现(与 key 验证相关)有什么区别?使用 IssuerSigningKeys = openidconfig.SigningKeys 下载和提供 key ,但为什么它没有通过 AddJwtBearer< 使用 .well-known/openid-configuration 自动加载 中间件?

关于c# - IDX10501 : Signature validation failed. 无法匹配 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com