【问题标题】:Identityserver 4 in docker compose The remote certificate is invalid according to the validation procedureIdentityserver 4 in docker compose 远程证书根据验证程序无效
【发布时间】:2021-06-05 08:04:29
【问题描述】:

我正在尝试将 IdentityServer 4 配置为在 Docker 中工作,身份服务器容器本身正在运行,但我无法从客户端连接到它。

我的申请包括:

  • API
  • 客户
  • 身份服务器4

我的 docker compose 看起来像

services:
 client:
  image: ${DOCKER_REGISTRY-}client
  ports:
    - '6001:80'
  build:
   context: .
   dockerfile: UI/client.UI/Dockerfile
  depends_on:
        - db

identityserver:
 image: ${DOCKER_REGISTRY-}identityserver
 ports:
  - '5001:443'
 build:
  context: .
  dockerfile: Security/IdentityServer/Dockerfile
 depends_on:
        - db

apiservice:
 image: ${DOCKER_REGISTRY-}apiservice
 build:
  context: .
  dockerfile: API/Services/api.Service/Dockerfile
 depends_on:
        - db

db:
  image: "mcr.microsoft.com/mssql/server:2019-latest"
  environment:
      SA_PASSWORD: "PaSSw0rd"
      ACCEPT_EULA: "Y"
      MSSQL_PID: Express
  ports:
      - "1433:1433"
  volumes: 
     - mssql-volume:/var/opt/mssql

 networks:
   default:
      driver: bridge

 volumes:
     mssql-volume:

我的身份服务器StartUp 类:

 public void ConfigureServices(IServiceCollection services)
 {
    ....

    var builder = services.AddIdentityServer(options =>
        {
            options.Events.RaiseErrorEvents = true;
            options.Events.RaiseInformationEvents = true;
            options.Events.RaiseFailureEvents = true;
            options.Events.RaiseSuccessEvents = true;
            options.UserInteraction.LoginUrl = "/Account/Login";
            options.UserInteraction.LogoutUrl = "/Account/Logout";
            options.Authentication = new AuthenticationOptions()
            {
                CookieLifetime = TimeSpan.FromHours(10), // ID server cookie timeout set to 10 hours
                CookieSlidingExpiration = true
            };
            options.IssuerUri = "https://172.20.16.1:5001";
        })
        .AddConfigurationStore(options => // this adds the config data from DB (clients, resources)
        {
            ...
        })
        .AddOperationalStore(options =>// this adds the operational data from DB (codes, tokens, consents)
        {
            ...
        })
        .AddDeveloperSigningCredential()
        .AddAspNetIdentity<ApplicationUser>();
 }

我的客户StartUp类:

 public void ConfigureServices(IServiceCollection services)
 {
     services.AddAuthentication(options =>
        {
            options.DefaultScheme = "cookie";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("cookie")
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "https://172.20.16.1:5001";
            options.RequireHttpsMetadata = false;

            options.ClientId = "ClientMVC";
            options.ClientSecret = "SuperSecretPassword";

            options.SaveTokens = true;

            options.ResponseType = "code";
            options.UsePkce = true;
            options.ResponseMode = "query";

            options.GetClaimsFromUserInfoEndpoint = true;

            options.Scope.Clear();
            options.Scope.Add("openid");
            .. some scopes
        });
 }

我收到以下错误

AuthenticationException:根据验证程序,远程证书无效:
远程证书名称不匹配,远程证书链错误
System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken 消息,ExceptionDispatchInfo 异常)

HttpRequestException:无法建立 SSL 连接,请参阅内部异常。
System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(bool async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancelToken)

IOException:IDX20804:无法从“System.String”检索文档。

Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(字符串地址,CancellationToken 取消)

InvalidOperationException:IDX20803:无法从“System.String”获取配置。

Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken cancel)

我尝试使用以下命令添加证书

dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\IdentityServer.pfx -p passw0rd!
dotnet dev-certs https --trust

但这会将证书添加到localhost,这在这种情况下无效。

有什么帮助吗?

【问题讨论】:

    标签: docker docker-compose asp.net-core-mvc microservices identityserver4


    【解决方案1】:

    你不能使用这样的 URL:https://172.20.16.1:5001

    HTTPS 需要证书,因此您需要一个域名。当我将 IdentityServer 部署为容器(Azure Container Registry)时,我将签名密钥、数据保护密钥和 HTTPS 证书放在 Azure Key Vault 中。

    【讨论】:

    • 现在我想为开发配置它。如何解决?
    • 作为一个起点,尽量不要在开发中使用HTTPS,先在本地不使用HTTPS,然后尝试通过证书掌握它。要使证书生效,您需要为指向您的身份服务器 docker 容器的域创建证书。
    • 谢谢,会试试这个
    【解决方案2】:

    感谢@Tore 在回答中提供的有用提示

    我按照以下步骤使身份服务器在 docker 中工作

    1. 更改identityserver的配置使其在iis中工作 没有SSL
    2. 然后更改docker的配置 容器和docker compose 通过删除任何SSL 相关 配置。
    3. docker compose 运行应用程序并 一切正常
    4. 对 配置以确保这将在生产中更改为 包括SSL 证书。

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 2019-01-30
      • 2011-03-28
      • 2016-11-28
      • 2013-08-08
      • 2012-07-20
      • 2021-05-30
      相关资源
      最近更新 更多