DiscoveryClient.GetAsync 在调用通过 SSL 终止器时失败“颁发者名称与权限不匹配”



我们的 Web 应用程序托管在 SSL 终止集群后面。我打电话给DiscoveryClient.GetAsync

var discoveryClient = DiscoveryClient.GetAsync("https://ourcluster.net/identityserver").Result;

我得到以下结果:

颁发者名称与权限不匹配:http://ourcluster.net/identityserver

我假设SSL终止导致身份服务器端点以http格式接收请求,从而相应地解析其权限URL。

关于如何解决此问题的任何建议?

-S

出现这种情况的原因是 http 请求来自处于 SSL 终止状态的反向代理。因此,请求在代理上以https的形式发送,然后以http的形式转到IdentityServer4。

首先,在身份服务器中,您必须使用 MS 标头中间件,如@leastprivilege所述。您可以使用"Microsoft.AspNetCore.HttpOverrides"包中的此代码段。

using Microsoft.AspNetCore.HttpOverrides;
....
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    // UseForwardedHeaders must be BEFORE the UseIdentityServer.
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedProto
    });
    app.UseIdentityServer();
}

其次配置您的反向代理以将X-Forwarded-Proto发送到上游。我们使用nginx,所以最低配置必须是:

server {
    listen 80;
    listen [::]:80;
    listen 443 default_server ssl;
    root /var/www;
    server_name id.example.com;
    ssl on;
    ssl_certificate    /opt/certs/2017/identity-server/fullchain.pem;
    ssl_certificate_key   /opt/certs/2017/identity-server/privkey.pem;
    access_log /var/log/nginx/id.example.com-access.log main;
    error_log  /var/log/nginx/id.example.com-error.log;
    location / {
            proxy_pass http://identity-service;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
            proxy_set_header Host $host;
            proxy_redirect off;
    }
}

通过在托管身份服务器端点的应用程序中设置 IssuerUri,我们找到了可接受的解决方法:

services.AddIdentityServer(options =>
{
    var issuerUri = Configuration["IdentityServer:IssuerUri"];
    if (!string.IsNullOrEmpty(issuerUri))
    {
        options.IssuerUri = issuerUri;
    }
});

因此,客户端应用程序从 JWT 和它们自己对 IdentityServer 终结点的调用中获得相同的 issuerUri 值。

-S

最新更新