【问题标题】:AutomaticAuthenticate for ASP.NET IdentityServer4 on open routes?AutomaticAuthenticate for ASP.NET IdentityServer4 on open routes?
【发布时间】: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


【解决方案1】:

UseJwtBearerAuthentication 已过时。在 ConfigureServices 中使用 AddAuthentication().AddJwtBearer 配置 JwtBearer 身份验证。

还要确保 app.UseAuthentication();已添加到您的启动类中。

我发现一个最佳实践是始终将 IdentityServer、客户端和 API 托管在单独的 ASP.NET Core 实例上,否则很难推断出什么是什么。 IdentityServer 发出自己的用户和 cookie,所以我认为您的系统对使用什么用户感到困惑?

参考:

【讨论】:

    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2015-07-01
    • 2020-09-30
    • 2018-03-06
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多