【问题标题】:IdentityServer4 token signing validationIdentityServer4 令牌签名验证
【发布时间】:2017-11-19 07:37:55
【问题描述】:

我有生成签名 JWT 令牌的 IdentityServer4。 在我的 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 时返回正确的用户

【问题讨论】:

    标签: c# asp.net-core jwt identityserver4


    【解决方案1】:

    最后我用 JwtBearerAuthentication 做到了,

    GetUserAsync 函数失败可以通过调用来修复:

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
    

    因为这个问题:https://github.com/aspnet/Security/issues/1043

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 2018-05-29
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2019-02-07
      相关资源
      最近更新 更多