【问题标题】:AddOpenIdConnect options.Authority overriden with current host behind proxy (UseForwardedHeaders)AddOpenIdConnect options.Authority 被代理后面的当前主机覆盖 (UseForwardedHeaders)
【发布时间】:2021-03-31 17:57:58
【问题描述】:

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

.NET Core 版本: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; 
    });

我的控制器动作:

[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 的登录页面,但它会使用以下 URL 将我重定向回我的主机: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 授权地址主机。

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

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      相关资源
      最近更新 更多