Identity Server 4和Docker

我试图configurationDocker的IdentityServer4,但我不能让它的工作。 为了开始,我使用了身份服务器文档的Client Credential示例: 使用Client Credentials保护API

IdentityServer
托pipe在5000端口上

的WebAPI
托pipe在港口5001

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

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

客户
和客户

  // 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"); 

问题可能是在我的WebApi:

1)如果我把权限设置为localhost:5000,我得到一个内部服务器错误:“无法获取configuration:' http:// localhost:5000 / .well-known / openid-configuration '”这是有道理的,因为本地主机:5000在这个容器中是未知的

2)如果我把权限设置为http:// web:5000,我得到一个授权错误:“颁发者validation失败,颁发者:' http:// localhost:5000 ',不匹配:validationParameters.ValidIssuer:' http:/ /networking:5000 '或validationParameters.ValidIssuers“这也有道理,但我不知道是否有可能更改权威名称? 我也尝试在IdentityServer项目中设置IssuerUri ,但没有帮助

networking

假设你有两台物理机器:C1和C2。 每台机器都是docker主机。

C1运行Auth容器。

C2运行WebApi容器。

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

有很多networking问题可以用来实现这个目标。 例如: 什么会阻止Docker容器中运行的代码连接到单独服务器上的数据库?

发行人validation

发行人validation失败

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

issuer财产价值取决于您的客户的networking请求:

 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" 

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

 services.AddIdentityServer(x => { x.IssuerUri = "foo"; }) 

实现issuer价值不变。 这允许通过任何有效的URL(使用IP,计算机名称或DNS)来呼叫Identity Server:

 GET http://anything/.well-known/openid-configuration -> "issuer": "foo" 

DiscoveryClient也validationissuer价值。 这是一个简单的平等比较 :

 public bool ValidateIssuerName(string issuer, string authority) { return string.Equals(issuer, authority, StringComparison.Ordinal); } 

你可以禁用它:

 DiscoveryClient.Policy.ValidateIssuerName = false; 

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

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