【问题标题】:IdentityServer4 + MVC client in DockerDocker 中的 IdentityServer4 + MVC 客户端
【发布时间】:2021-05-03 08:45:21
【问题描述】:

我想在 Docker 中部署 IdentityServer4 和 MVC 客户端。作为反向代理,我使用 NGINX。但是我的容器无法访问外部网络。 (下图红线)

我的 MVC 客户端应用程序通过内部网络请求发现文档并接收内部 URI。但是它们被浏览器中的客户端代码使用。 FQDN 可以是带有端口或常规域名的 IP 地址。需要做什么来解决我的问题? 我找到了similar question。建议使用相同的 DNS 名称并将其在 docker-compose 网络中解析为 IdentityServer4 容器。但是在IP地址的情况下怎么办? this answer 也有同样的问题。 我什至考虑过迁移到 oidc-client.js。但这会花费太多时间。

MVC 客户端:

services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = "http://fqdn/idsrv";
                options.RequireHttpsMetadata = false;

                options.ClientId = "mvc";
                options.SaveTokens = true;
            });

IdentityServer4

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);

        var builder = services.AddIdentityServer(o => {

        })
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApis())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetUsers());

        builder.AddDeveloperSigningCredential();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UsePathBase("/idsrv");
        app.UseDeveloperExceptionPage();

        app.UseStaticFiles();

        app.UseIdentityServer();

        app.UseMvcWithDefaultRoute();
    }

docker-compose:

version: "3.9"
services: 
mvcclient:
    build: 
        context: .
        dockerfile: \MvcClient\Dockerfile 
identityserver:
    build: 
        context: .
        dockerfile: \IdentityServer\Dockerfile
proxy:
    image: nginx
    ports:
        - "80:80"

NGINX:

location ~* ^/idsrv {
        proxy_pass http://identityserver;
    }
location ~* ^/mvcclient {
        proxy_pass http://mvcclient;
    }

【问题讨论】:

    标签: docker asp.net-core authentication identityserver4 openid-connect


    【解决方案1】:

    不是最好的解决方案,但它确实发生了。

    MVC 客户端:

    .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "http://{public_fqdn}/idsrv";
            options.MetadataAddress = "http://{idsrv_container_name}/.well-known/openid-configuration";
            options.TokenValidationParameters.ValidIssuer = "http://{public_fqdn}/idsrv";
    
            options.Events.OnRedirectToIdentityProvider = context =>
            {
                // Intercept the redirection so the browser navigates to the right URL in your host
                context.ProtocolMessage.IssuerAddress = "http://{public_fqdn}/idsrv/connect/authorize";
                return Task.CompletedTask;
            };
    
            options.Events.OnRedirectToIdentityProviderForSignOut = context =>
            {
                // Intercept the redirection so the browser navigates to the right URL in your host
                context.ProtocolMessage.IssuerAddress = "http://{public_fqdn}/idsrv/connect/endsession";
                return Task.CompletedTask;
            };
    
            options.ClientId = "mvc";
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多