【发布时间】:2021-05-29 11:03:57
【问题描述】:
我在托管 IdentityServer 的 ASP.NET 核心应用程序中有几个端点。 当我向客户端发出 JWT 令牌,并且该客户端以 JWT 作为不记名令牌调用 [AllowAnonymous] 端点时,用户主体为空。看起来令牌没有被任何中间件解析,并且似乎没有指定始终尝试解析 JWT 的选项。
有没有办法自动处理这个问题,还是我需要使用 .AddJwtBearer 扩展名?如果是后者,我似乎找不到一种简单的方法来填充签名密钥,使其与为身份服务器配置的签名密钥匹配。
var tokenValidationParameters = new TokenValidationParameters
{
// The signing key must match!
ValidateIssuerSigningKey = true,
IssuerSigningKey = ?? signing keys from Identity Server,
// Validate the JWT Issuer (iss) claim
ValidateIssuer = true,
ValidIssuer = // I guess this is typically the public URL for this instance,
// Validate the token expiry
ValidateLifetime = true,
// If you want to allow a certain amount of clock drift, set that here:
ClockSkew = TimeSpan.Zero
};
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
我猜想我可以通过 x509SecurityKey 类通过我在 IdentityServer 上配置的 X509 传递 SigningKey
【问题讨论】:
-
你添加了app.UseAuthentication();到客户端启动类?
-
是的,我有 app.UseIdentityServer();应用程序.UseAuthentication()。配置服务时,我设置 IdentityServer (services.AddIdentityServer(....)) 然后调用 services.AddAuthentication().AddJwtBearer(opt => {});仍然当我提供不记名令牌时,用户只是未受保护端点上的默认用户
-
更新了我的答案
标签: asp.net-core identityserver4