如何解决AddOpenIdConnect options.Authority 被代理后面的当前主机覆盖UseForwardedHeaders
我在容器内的反向代理后面托管我的应用程序。我正在使用 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 Connect 配置:
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 授权地址主机。
你能帮我吗,我不明白为什么它把我重定向回我的主机?
谢谢。
解决方法
我发现了这个问题,这里是解决方案,以防万一有人在他们的本地设置中使用 IIS 作为反向代理。
该问题是由我在本地使用的 IIS 反向代理配置引起的 - 应用程序请求路由 -> 服务器代理设置 -> 响应标头中的反向重写主机。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。