ASP.Net 身份重定向到登录在代理后面不起作用

问题描述 投票:0回答:0

我有一个 YARP 反向代理设置,具有非常基本的配置,可以在传递到 Web 应用程序之前将请求直接传递到 nginx 反向代理容器(其中验证 modsecurity 规则)。它们都驻留在 Azure 中。

将 YARP 作为附加反向代理的原因只是为了提供一个公共静态 IP,该 IP 允许 nginx 容器及其卷安装资源安全地生活在不允许使用公共 IP 地址的虚拟网络中。

该 Web 应用程序是具有身份的旧版 .Net 4 Webforms 应用程序。除非用户尝试直接访问需要授权的页面,否则一切都有效。通常的行为是重定向到登录页面,然后将其返回到他们尝试访问的页面。问题是在这种情况下它会尝试使用代理主机加载页面:

前往

https://targethost.com/restrictedpage
会重定向至
https://nginxhost.com/login.aspx?returnUrl=/restrictedpage
,而不是
https://targethost.com/login.aspx?returnUrl=/restrictedpage

如果我先通过

https://targethost.com/login.aspx
登录,它会起作用,然后我可以转到
https://targethost.com/restricted

这是 YARP 中的基本路由配置:

  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "route1": {
        "ClusterId": "cluster1",
        "Match": {
          "Path": "{**catch-all}",
          "Hosts": [ "targethost.com" ]
        },
      }
    },
    "Clusters": {
      "cluster1": {
        "Destinations": {
          "destination1": {
            "Address": "http://nginxproxy.com:80/"
          }
        }
      }
    }
  }

nginx代理配置如下:

server {
    listen 80 default_server;

    server_name nginxproxy.com;
    set $upstream https://webapp.com;
    set $always_redirect off;

    location / { ...
       proxy_set_header Host $host;    
       proxy_set_header Proxy "";
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;
       proxy_set_header X-REAL-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Port $server_port;
       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_redirect off;

       proxy_pass_header Authorization;
       proxy_pass $upstream;

       set_real_ip_from 10.5.3.254;
       set_real_ip_from 127.0.0.1;

       real_ip_header X-REAL-IP;
       real_ip_recursive on;.

这是处理它的网络应用程序中的配置:

app.UseCookieAuthentication(New CookieAuthenticationOptions() With {
    .ExpireTimeSpan = TimeSpan.FromMinutes(5),
    .SlidingExpiration = True,
    .AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    .Provider = New CookieAuthenticationProvider() With {
        .OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(Of ApplicationUserManager, ApplicationUser)(
            validateInterval:=TimeSpan.FromMinutes(30),
            regenerateIdentity:=Function(manager, user) user.GenerateUserIdentityAsync(manager))},
    .LoginPath = New PathString("/Account/Login")})

考虑到在没有重定向登录的情况下一切都按预期工作,我猜测这与重定向登录的工作方式以及它使用的主机有关。如果问题所在,我希望能够覆盖重定向到登录过程。

有什么可能导致此问题或如何解决它吗?

更新1

以下是 Web 应用程序启动中的代码,用于配置 OWIN 中间件和登录页面以将未经身份验证的用户重定向到:

公共子配置(app As IAppBuilder)

Dim c = New CookieAuthenticationOptions
c.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
c.
c.LoginPath = New PathString("/account/login")
app.UseCookieAuthentication(c)

根据文档(这是 .net core doco。我假设没有任何更改),在处理 ChallengeAsync 时,LoginPath 属性由处理程序用于重定向目标。当前 url 作为由 ReturnUrlParameter 命名的查询字符串参数添加到 LoginPath。一旦对 LoginPath 的请求授予新的登录身份,ReturnUrlParameter 值将用于将浏览器重定向回原始 url。

nginx-reverse-proxy asp.net-identity-2 ms-yarp
© www.soinside.com 2019 - 2024. All rights reserved.