繁体   English   中英

IdentityServer4令牌签名验证

[英]IdentityServer4 token signing validation

我有IdentityServer4生成签名的JWT令牌。 在我的Web API中,我添加了身份验证中间件来验证这些令牌:

         app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            AllowedScopes = { "WebAPI", "firm",
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile },
            RequireHttpsMetadata = env.IsProduction(),
        });

它运作完美。 但是,我怀疑它没有验证jwt令牌的签名,因为没有配置用于验证令牌的公共密钥。 如何配置令牌签名验证?

PS:我尝试通过这种方式使用UseJwtBearerAuthentication:

        var cert = new X509Certificate2("X509.pfx", "mypassword");
        var TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            ValidateIssuer = true,
            ValidIssuer = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            IssuerSigningKey = new X509SecurityKey(cert),
        };
        app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            Authority = env.IsProduction() ? "https://www.wigwam3d.com/api/" : "http://localhost/api/",
            Audience = "WebAPI",
            RequireHttpsMetadata = env.IsProduction(),
            TokenValidationParameters = TokenValidationParameters
        });

它也可以工作(我希望也可以验证令牌签名!),但是给了我另一个错误:

UserManager.GetUserAsync(HttpContext.HttpContext.User)

返回null,在使用UseIdentityServerAuthentication时返回正确的用户

我认为无需向您的API添加证书以进行验证。 .UseIdentityServerAuthentication()中间件在启动时会从https://www.example.com/api/.well-known/openid-configuration调用IdentiyServer检索公共密钥。 至少那是我的理解。

最后,我用JwtBearerAuthentication做到了,

GetUserAsync函数失败可以通过调用以下方法解决:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

由于这个问题: https : //github.com/aspnet/Security/issues/1043

欢迎使用IdentityServer auth配置相同的任何想法!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM