Identity Server 4和docker

Bid*_*dou 9 c# docker .net-core identityserver4

我正在尝试使用docker配置IdentityServer4,但我无法使其工作.首先,我获取了身份服务器文档的Client Credential示例:使用Client Credentials保护API

IdentityServer
托管在端口5000上

WebApi
托管在端口5001上

在我的WebApi文件的Configure方法中,Startup.cs我做了以下(问题可能在这里):

 app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = "http://web:5000",                
            RequireHttpsMetadata = false,
            ApiName = "api1"
        });
Run Code Online (Sandbox Code Playgroud)

客户
和客户

 // Everything is fine here...
 var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
 var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
 var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api");

 // This does not work
 var client = new HttpClient();
 client.SetBearerToken(tokenResponse.AccessToken);
 var response = await client.GetAsync("http://localhost:5001/identity");
Run Code Online (Sandbox Code Playgroud)

问题可能出在我的WebApi中:

1)如果我将权限设置为localhost:5000,我收到一个内部服务器错误:"无法从以下地址获取配置:' http:// localhost:5000/.well-known/openid-configuration '"这是有道理的,因为localhost :5000在此容器中未知

2)如果我将权限设置为http:// web:5000,我会收到授权错误:"颁发者验证失败.发行者:' http:// localhost:5000 '.不匹配:validationParameters.ValidIssuer:' http://web:5000 '或validationParameters.ValidIssuers"这也有意义,但我不知道是否可以更改权限名称?我也尝试IssuerUri在IdentityServer项目中设置,但它没有帮助

Ily*_*kov 11

网络

假设您有两台物理机器:C1和C2.每台机器都是一个泊坞主机.

C1运行Auth容器.

C2运行WebApi容器.

当您在Auth dockerfile中公开端口5000时,该地址C1:5000应该可以从C2 WebApi容器本身访问.您可能更喜欢IP到DNS,这没关系.此外,您应该能够成功地获得GET请求http://C1:5000/.well-known/openid-configuration.

为实现这一目标,您可能面临许多网络问题.例如: 什么会阻止在Docker容器中运行的代码连接到单独服务器上的数据库?

发行人验证

颁发者验证失败

您的客户端权限URL与Auth主机名不同.默认情况下,权限URL应等于issuer属性值(此属性在Identity Server自动发现文档响应中).

issuer 属性值取决于客户的Web请求:

GET http://127.0.0.1:6000/.well-known/openid-configuration -> "issuer": "http://127.0.0.1:6000"
GET http://localhost:6000/.well-known/openid-configuration -> "issuer": "localhost:6000"
Run Code Online (Sandbox Code Playgroud)

尝试IssuerUri为开发环境设置常量:

services.AddIdentityServer(x =>
{
    x.IssuerUri = "foo";
})
Run Code Online (Sandbox Code Playgroud)

实现恒定的issuer价值.这允许通过任何有效的URL(使用IP,机器名或DNS)呼叫Identity Server:

GET http://anything/.well-known/openid-configuration -> "issuer": "foo"
Run Code Online (Sandbox Code Playgroud)

DiscoveryClient也验证了issuer价值.这是一个简单的平等比较:

public bool ValidateIssuerName(string issuer, string authority)
{
    return string.Equals(issuer, authority, StringComparison.Ordinal);
}
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式禁用它

DiscoveryClient.Policy.ValidateIssuerName = false;
Run Code Online (Sandbox Code Playgroud)

仅供参考,IssuerUri设置,不建议用于生产环境:

IssuerUri设置将在发现文档和已颁发的JWT令牌中显示的颁发者名称.建议不要设置此属性,该属性从客户端使用的主机名中推断颁发者名称.