首页 > 解决方案 > 使用 RunProxy 和 OpenIdConnect 时的无限身份验证循环

问题描述

在解决这个问题之前——我们如何解决无限认证循环——一些关于架构的信息。

我们正在使用 .net 核心 2.1。

我们有 2 项服务。第一个是面向公共流量的,是否终止 TLS 并确定是否应该传递请求。(可能对其他服务器)当该服务器发现请求是针对某个路径发出的,它使用RunProxy方法将请求映射到使用 http 的“其他”服务。该代码如下所示:

app.MapWhen(<MatchRequestCondition>, proxyTime => proxyTime.RunProxy(
                new ProxyOptions
                {
                    Scheme = "http",
                    Host = "localhost",
                    Port = "1122"
                }
            ));

例如,如果您访问https://localhost:1234/abc - 这将映射到http://localhost:1122 - 这是第二个应用程序所在的端口。

现在,此辅助服务使用 OpenIdConnect - 它的配置如下所示。

// Configure Services method
services.AddMvc(mvcOptions => {
AuthorizationPolicy policy = new AuthorizationPolicyBuilder()
                        .RequireAuthenticatedUser()
                        .Build();
                        mvcOptions.Filters.Add(new AuthorizeFilter(policy));
});

services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(auth =>
            {
                auth.ClientId = "<client_id>";
                auth.ClientSecret = "<client_secret>";
                auth.Authority = "<authority>";
            });


// Configure method
app.UseAuthentication();

这就是有趣的地方:

如果我直接访问第二个节点(仅用于接收来自第一个节点的流量) - 例如http://localhost:1122 - 我将被重定向到登录并且一切正常。

但是如果我访问第一个节点(这是真正的流量应该来自的节点) - 它会进入一个疯狂的身份验证循环。

有什么想法可能是根本原因吗?这与在常规服务前使用负载均衡器有何不同?或者可能是因为我在辅助服务中使用了 cookie 中间件?

标签: asp.net-coreasp.net-authentication

解决方案


推荐阅读