我在 IIS 服务器上有一个 ASP.NET 核心应用程序,侦听 whatever.domain.com/virtual-dir 使用 .外界通过 SSL 加速器访问它。它使用Microsoft.AspNetCore.Authentication.OpenIdConnect通过AzureAd进行OpenId身份验证。
由于SSL代理,应用程序的东西它的基址方案是http而不是https(http://whatever.domain.com/virtual-dir
与https://whatever.domain.com/virtual-dir
。这导致它发送http://whatever.domain.com/virtual-dir/signin-oidc
而不是https://whatever.domain.com/virtual-dir/signin-oidc
的回复地址。我可以使用OpenIdConnectOptions.CallbackPath修改回调端点,但这仅相对于基本URL而言。如何更改基本网址?
不要这样做。这是个坏主意。
到目前为止,我想出如何做到这一点的最好方法是将OpenIdConnectOptions.Events.OnRedirectToIdentityProvider设置为一个函数,在该函数中,您可以在传递的RedirectContext参数中编辑ProtocolMessage.RedirectUri。
/// <seealso cref="OpenIdConnectEvents.OnRedirectToIdentityProvider"/>
public async Task OnRedirectToIdentityProvider(RedirectContext redirectContext)
{
redirectContext.ProtocolMessage.RedirectUri =
"https://whatever.domain.com/virtual-dir";
}
这方面的说明在这里:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1#when-it-is-possible-to-add-forwarded-headers-and-all-requests-are-secure
故障:
- 您的代理需要设置三个 Http 标头,
X-Forwarded-For
、X-Forwarded-Proto
和X-Forwarded-Host
- 您需要配置 ForardHeaderOptions。理论上,IIS 应该为您执行此操作。
- 在
Startup.Configure()
中,您需要在app.UseAuthentication()
之前调用app.UseForwardedHeaders();
,以便重写Request
基。