簡體   English   中英

如何將 IdentityServer 與 docker-compose 一起使用?

[英]How to use IdentityServer with docker-compose?

Ciao,我正在使用以下三個組件開發一個 web 應用程序:

  • 身份服務器:使用身份服務器4
  • Web API :使用 ASP.NET 內核 5
  • Web 應用程序:使用 ASP.NET Blazor(服務器端)。

我正在使用 Windows 和 Docker 用於桌面版本 20.10.5,構建 55c4c88。

我將調試和部署我的應用程序組件作為 Docker 容器。 對於每個組件,我都添加了 Dockerfile 並添加了對 docker-compose 的解決方案支持。

每個 Dockerfile 都暴露端口 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 App:

<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));
        });
    }

我使用 docker-compose 通過 Visual Studio 2019 啟動應用程序。 當我嘗試啟動應用程序時,我收到一個錯誤,如下所示:

SocketException:連接被拒絕 System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError 錯誤,CancellationToken cancelToken)

HttpRequestException: 連接被拒絕 (identityserver:443) System.Net.Http.ConnectHelper.ConnectAsync(Func<SocketsHttpConnectionContext, CancellationToken, ValueTask> 回調, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancelToken)

IOException:IDX20804:無法從“System.String”檢索文檔。 Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(字符串地址,CancellationToken 取消)

InvalidOperationException:IDX20803:無法從“System.String”獲取配置。 Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken 取消)

注意:我可以通過 url 及其端口直接訪問 IdentityServer 的主頁。

我確定客戶端配置正確,因為之前使用容器授權可以正常工作。

我該如何解決這個問題?

十分感謝

再見,

我的問題與 docker 無關,而是由 HTTPS 引起的。 我懷疑使用別名使 Visual Studio 生成的開發 HTTPS 證書無效。

I've checked this adding these lines of codes for each component (identity server, web api and web app) and using HTTP protocol:

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

在 HTTP 中導航並更改身份服務器配置我可以查看登錄頁面。 這不是最終解決方案,但讓我檢查一下問題是否與 HTTPS 證書有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM