簡體   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