Identity Server 4 在负载均衡器后面运行

Ant*_*rps 5 .net c# asp.net-identity asp.net-core

我已经使用实体框架为我的项目设置了 Identity Server 4。我已经将服务配置为使用持久授权存储和签名证书。

services.AddIdentityServer()
        .AddSigningCredential(Config.GetSigningCertificate())
        .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
        .AddProfileService<ProfileService>()
        .AddConfigurationStore(builder =>
                    builder.UseSqlServer(connectionString, options =>
                        options.MigrationsAssembly(migrationsAssembly)))
        .AddOperationalStore(builder =>
                    builder.UseSqlServer(connectionString, options =>
                        options.MigrationsAssembly(migrationsAssembly)));
Run Code Online (Sandbox Code Playgroud)

这是服务的配置。

问题是,当我在负载均衡器后面运行服务器(例如,有 2 个相同的实例处理所有请求)时,用户未登录的服务器无法解码 JWT 令牌,从而导致 401 未经授权的错误。

我假设令牌的签名方法或其加密是问题所在,但我找不到解决此问题的方法。

这是我的其余配置。

配置:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
      Authority = url,
      // Authority = "http://localhost:5000",
      AllowedScopes = { "WebAPI" },
      RequireHttpsMetadata = false,
      AutomaticAuthenticate = true,
      AutomaticChallenge = true,

});
Run Code Online (Sandbox Code Playgroud)

客户端:

new Client
{
     ClientId = "Angular2SPA",
     AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, // Resource Owner Password Credential grant.
     AllowAccessTokensViaBrowser = true,
     RequireClientSecret = false, // This client does not need a secret to request tokens from the token endpoint.
     AccessTokenLifetime = 7200, // Lifetime of access token in seconds.
     AllowedScopes = {
                       IdentityServerConstants.StandardScopes.OpenId, // For UserInfo endpoint.
                       IdentityServerConstants.StandardScopes.Profile,
                       "roles",
                       "WebAPI"
                      },
     AllowOfflineAccess = true, // For refresh token.
     AccessTokenType = AccessTokenType.Jwt

}
Run Code Online (Sandbox Code Playgroud)

我还实现了自己的 IResourceOwnerPasswordValidator 和 IProfileService。

知道为什么会发生这种情况吗?

CaP*_*ter 2

我遇到了类似的问题,负载平衡 Identity Server 4 并能够使用 Startup.cs 的 ConfigureServices 中的 .AddDataProtection() 共享密钥。

public void ConfigureServices(IServiceCollection services)
{
// Other service configurations

  services.AddDataProtection();

// Additional service configurations    
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果您选择这条路线,请考虑使用类似的扩展(有 .ProtectKeysWith*多个选项)来加密这些密钥(无论您决定使用哪种介质)。请参阅https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-2.1有关详细信息,

华泰