【问题标题】:IdentityServer4 within Docker container - Exception IDX20803: Unable to obtain configuration from: 'System.String'Docker 容器中的 IdentityServer4 - 异常 IDX20803:无法从“System.String”获取配置
【发布时间】:2020-11-25 02:17:51
【问题描述】:

我一直在实施 IdentityServer4 来为我的 Angular 应用程序提供授权。我在我的本地开发环境中工作,但是在 Docker 容器中运行时遇到了问题。我可以通过 /connect/token 端点生成访问令牌,但是当我尝试使用该令牌访问受保护的 API 时,出现以下异常:

System.InvalidOperationException:IDX20803:无法从“System.String”获取配置。 System.IO.IOException:IDX20804:无法从以下位置检索文档:“System.String” System.Net.Http.HttpRequestException:无法分配请求的地址 System.Net.Sockets.SocketException (99):无法分配请求的地址 在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32 端口,CancellationToken 取消令牌)

我认为这可能在某处验证失败,但我尝试将所有这些选项设置为 false,但没有任何效果:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Authority = this.Configuration.SiteUrl;
        options.RequireHttpsMetadata = this.Configuration.SiteUrl.StartsWith("https://");
                    
        options.TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = Claims.Username,
            RoleClaimType = Claims.Role,
            RequireExpirationTime = true,
            ClockSkew = TimeSpan.FromMinutes(1),
            ValidateActor = false,
            ValidateTokenReplay = false,
            ValidateIssuer = false,
            ValidateAudience = false,
            ValidateLifetime = false,
            ValidateIssuerSigningKey = false
        };
    });

如果重要的话,我的测试是在容器内部和外部使用非 https 的 Configuration.SiteUrl 值进行的,所以这不是罪魁祸首。我真的不认为这是一个配置问题,因为它成功生成了令牌,并且相同的代码在 Docker 容器之外正常工作。

Configuration.SiteUrl 在 Docker 容器中设置为 http://localhost:8077。对 http://localhost:8077/.well-known/openid-configuration 的请求会返回正确的响应。根据错误消息,我认为它可能出于某种原因向容器内的端口 8077 发出请求,但是当我尝试将设置设置为 http://localhost:80 时没有任何变化(因为在容器内使用了端口 80)。

任何识别/解决问题的帮助将不胜感激,因为我已经为此苦苦挣扎了几个小时。

【问题讨论】:

    标签: c# asp.net identityserver4


    【解决方案1】:

    事实证明,我的一次尝试走在了正确的轨道上。问题是权限需要在容器内解析,所以它必须使用内部端口(对于 ASP.NET Core 是 80)。发行人应公开解决,这需要额外的设置。

    在配置服务中:

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.Authority = this.Configuration.Authority;
            options.RequireHttpsMetadata = this.Configuration.SiteUrl.StartsWith("https://");
                        
            options.TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = Claims.Username,
                RoleClaimType = Claims.Role,
                RequireExpirationTime = true,
                ClockSkew = TimeSpan.FromMinutes(1),
                ValidateIssuer = true,
                ValidateAudience = false,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true
            };
        });
    

    在配置中:

    services.AddIdentityServer(options => options.IssuerUri = this.Configuration.SiteUrl)
        .AddResourceStore<IdentityResourceStore>()
        .AddClientStore<IdentityClientStore>()
        .AddPersistedGrantStore<IdentityPersistedGrantStore>()
        .AddResourceOwnerValidator<IdentityIResourceOwnerPasswordValidator>()
        .AddProfileService<IdentityProfileService>()
        .AddSecretValidator<IdentitySecretValidator>();
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2021-07-15
      • 2021-09-10
      • 1970-01-01
      • 2019-04-28
      • 2022-11-08
      • 2022-01-15
      • 2021-08-04
      • 2020-07-30
      相关资源
      最近更新 更多