如何将 IdentityServer 与 docker-compose 一起使用?

How to use IdentityServer with docker-compose?

再见, 我正在使用这三个组件开发一个 Web 应用程序:

我在 Windows 工作,Docker 桌面版 20.10.5,内部版本 55c4c88。

我会将应用程序的组件调试和部署为 Docker 容器。对于每个组件,我添加了一个 Docker 文件,并添加了对 docker-compose.

的解决方案支持

每个 Docker 文件公开端口 80 和 443。

...
EXPOSE 80
EXPOSE 443
...

我的 docker-compose 文件如下:

version: '3.4'

services:
  webapp:
    image: ${DOCKER_REGISTRY-}webapp
    ports: 
    - "44382:443"
    build:
      context: .
      dockerfile: WebApp/Dockerfile
    depends_on:
        - identityserver
        - webapi
    networks:
      - internal

  webapi:
    image: ${DOCKER_REGISTRY-}webapi
    ports:
    - "44305:443"
    build:
      context: .
      dockerfile: WebApi/Dockerfile
    depends_on:
      - identityserver
    networks:
        - internal

  identityserver:
    image: ${DOCKER_REGISTRY-}identityserver
    ports:
    - "443:443"
    build:
      context: .
      dockerfile: IdentityServer/Dockerfile
    networks:
        - internal

networks:
  internal:

我已经使用这两个包配置了带有 IdentityServer 的 Web 应用程序:

<PackageReference Include="IdentityModel" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.2" />

还有这些配置:

    public void ConfigureServices(IServiceCollection services)
    {
        // Adding my dependencies...
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            // Temporarly I've disabled HTTPS but it doesn't let work the project
            options.RequireHttpsMetadata = false;
         
            options.Authority = Configuration["OpenID:Authority"];
            options.ClientId = Configuration["OpenID:ClientId"];
            options.ClientSecret = Configuration["OpenID:ClientSecret"];
            options.ResponseType = "code";
            options.Scope.Add("WebApi");
            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;
            options.UsePkce = true;
            options.Events = new OpenIdConnectEvents
            {
                OnAccessDenied = context =>
                {
                    context.HandleResponse();
                    context.Response.Redirect("/");
                    return Task.CompletedTask;
                }
            };
        });

        services.AddMvcCore(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        });
    }

我在 Visual Studio 2019 年开始使用 docker-compose 申请。当我尝试启动应用程序时,出现如下错误:

SocketException: Connection refused System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

HttpRequestException: Connection refused (identityserver:443) System.Net.Http.ConnectHelper.ConnectAsync(Func<SocketsHttpConnectionContext, CancellationToken, ValueTask> callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)

IOException: IDX20804: Unable to retrieve document from: 'System.String'. Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)

InvalidOperationException: IDX20803: Unable to obtain configuration from: 'System.String'. Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken cancel)

注意:我直接通过url端口访问可以正确访问IdentityServer的主页

我确定客户端配置正确,因为之前使用容器授权工作正常。

我该如何解决这个问题?

非常感谢

再见,

我的问题与 docker 无关,而是由 HTTPS 引起的。我怀疑使用别名使 Visual Studio.

生成的开发 HTTPS 证书无效

我已经检查过为每个组件(身份服务器、Web api 和 Web 应用程序)添加这些代码行并使用 HTTP 协议:

if (!env.IsDevelopment())
{
    app.UseHttpsRedirection();
}

在 HTTP 中导航并更改 Identity Server 配置我可以查看登录页面。 这不是最终解决方案,但让我检查一下问题是否与 HTTPS 证书有关。