docker - AddOpenIdConnect options.Authority 被代理后面的当前主机覆盖(UseForwardedHeaders)

标签 docker asp.net-core identityserver4 reverse-proxy openid-connect

我在容器内的反向代理后面托管我的应用程序。我正在使用 IdentityServer4,我正在尝试使用 Azure AD 进行 oidc SSO。它在应用程序未使用 app.UseForwardedHeaders(opts) 时有效,但我需要它们。

.NET 核心版本:3.1

转发头配置:

var opts = new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto |
                           ForwardedHeaders.XForwardedHost
    };
opts.KnownProxies.Add(ipAddress);
app.UseForwardedHeaders(opts);

OpenID 连接配置:

services.AddAuthentication( options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}
    )
    .AddOpenIdConnect("aadrm", "Azure AD", options =>
    {
        options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
        options.SignOutScheme = IdentityServerConstants.SignoutScheme;

        options.Authority = "https://login.microsoftonline.com/{tenantID...}/v2.0";
        
        options.ClientId = ".....";
        options.ClientSecret = @"........";
        options.ResponseType = OpenIdConnectResponseType.CodeIdToken; 
    });

我的 Controller 操作:

[HttpGet]
public IActionResult ExternalLoginChallenge(string provider, string returnUrl)
{
    var callbackUrl = Url.Action(nameof(ExternalLoginCallback));

    var props = new AuthenticationProperties
    {
        RedirectUri = callbackUrl,
        Items =
        {
            { "scheme", provider },
            { "returnUrl", returnUrl }
        }
    };

    return Challenge(props, provider);
}

当我启动登录过程时,它应该将我重定向到 Microsoft 的登录页面,但它会将我重定向回我的主机,网址为:https://mylocalapp.com:443/{tenantId}/oauth2/v2.0/authorize?client_id=...&redirect_uri=HTTPS%3A%2F%2Fmylocalapp.com%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile&response_mode=form_post&nonce=........&state=............&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=6.8.0.0

当我删除 app.UseForwardedHeaders(opts) 时,它正常工作(将我重定向到 MS 登录页面)。似乎 UseForwardedHeaders 覆盖了 OpenIdConnect Authority Address 主机。

你能帮我吗,我不明白为什么它会将我重定向回我的主机?

谢谢。

最佳答案

我已经找到了这个问题,这里是解决方案,以防万一有人在他们的本地设置中使用 IIS 作为反向代理。

问题是由我在本地使用的 IIS 反向代理配置引起的 - 应用程序请求路由 -> 服务器代理设置 -> 反向重写响应 header 中的主机。

enter image description here

关于docker - AddOpenIdConnect options.Authority 被代理后面的当前主机覆盖(UseForwardedHeaders),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66867870/

相关文章:

c# - 带有警告和 InvalidOperationException 的 WCF WebService

asp.net-core - 是否可以公开来自 Asp.Net Core Identity 的安全标记作为 OIDC 的声明

linux - Docker 运行,设备上没有剩余空间

Docker Swarm 创建服务无法从远程存储库中提取

asp.net-core - 我们可以在 template.serverless 文件中声明阶段名称吗?

model-view-controller - IdentityServer 4 - 使用隐式授权类型时向用户添加自定义声明

c# - conditional-middleware-in-aspnet-core 在 asp.net core 1.1 中不工作

linux - 我正在尝试在 ubuntu 中打开 Excel 文件,但它显示垃圾字符

amazon-web-services - 如何仅使用Dockerrun.aws.json在ElasticBeanstalk Docker容器中装载卷

caching - 浏览器后退按钮不执行 Controller 方法