【问题标题】:IdentityServer4 - TokenEndpoint is always null when running in docker containerIdentityServer4 - 在 docker 容器中运行时,TokenEndpoint 始终为空
【发布时间】:2019-05-18 23:39:35
【问题描述】:

我在 docker swarm 容器中运行 IdentityServer4 (identity-service)。这是我的堆栈文件中的服务设置:

  identity-service:
    image: identity:${TAG:-latest}
    environment:
      - DATABASE_CONNECTION_STRING=mongodb://mongo:27017
      - ASPNETCORE_URLS=http://0.0.0.0:8080
      - LOGGING_LEVEL=1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    networks:
      - proxy
      - database
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
        failure_action: rollback
        order: start-first
      labels:
        - com.df.notify=true
        - com.df.servicePath=${SERVICE_PATH:-/identity}
        - com.df.port=8080
    healthcheck:
      test: ["CMD", "curl", "-f", "localhost:8080/health"]
      interval: 5s
      timeout: 10s
      retries: 10
      start_period: 10s

我也在使用IdentityModel2.Client 来请求令牌。这也通过另一个端点identity-serviceidentity-service 中完成:/user/login。这就是我使用DiscoveryClient的方式:

    var discoveryDocument = await new DiscoveryClient(hostUrl).GetAsync();
    if (discoveryDocument.IsError) {
        throw new Exception(
            String.Format("Failed to find discovery document at url: {0}, received following token-endpoint: {1}",
            hostUrl, discoveryDocument.TokenEndpoint));
    }
    return new TokenClient(discoveryDocument.TokenEndpoint, "Web", authorizationSecret);

如果我不在容器中运行它,这可以正常工作,但在 swarm 中运行时会抛出 Exception,因为由于某种原因,discoveryDocument.TokenEndpointnull。我还尝试将 shell 放入容器中,以查看是否可以使用 curl 访问 http://0.0.0.0:8080/.well-known/openid-configuration,我确实这样做了。

hostUrl 在容器中运行时为http://0.0.0.0:8080

【问题讨论】:

    标签: .net docker asp.net-core-2.0 identityserver4


    【解决方案1】:

    我通过不使用DiscoveryClient 解决了这个问题。相反,我将TokenClient 直接提供到令牌端点的 URL,如下所示:

    new TokenClient(String.Concat(hostUrl, "/connect/token"), "Web", authorizationSecret);
    

    hostUrl 由环境变量提供,并在我的 docker-stack 文件中设置为 http://127.0.0.1。出于某种原因,这行得通......

    【讨论】:

      【解决方案2】:

      我在客户端使用 IdentityServer 时遇到了同样的问题。 在 Startup 中,缺少 AddDeveloperSigningCredential 导致问题。

      【讨论】:

        猜你喜欢
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        • 2022-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-03-05
        相关资源
        最近更新 更多