gpt4 book ai didi

c# - IdentityServer4 token 签名验证

转载 作者:太空狗 更新时间:2023-10-29 23:12:41 28 4
gpt4 key购买 nike

我有生成签名 JWT token 的 IdentityServer4。在我的 web api 中,我添加了 auth 中间件来验证这些 token :

         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 token 的签名,因为没有配置公钥来验证 token 。如何配置 token 签名验证?

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
});

它也有效(我希望也能验证 token 签名!)但给了我另一个错误:

UserManager.GetUserAsync(HttpContext.HttpContext.User)

返回 null,而使用 UseIdentityServerAuthentication 返回正确的用户

最佳答案

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

关于c# - IdentityServer4 token 签名验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590719/

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