OpenIdConnect(Azure)设置
// COOKIES: Tells it to use cookies for authentication.
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieManager = new SystemWebCookieManager()
});
// OPEN-ID: Authenticate via Azure AD using OpenIdConnect.
//https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-webapi-openidconnect/
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
ClientId = ClientID,
Authority = Authority,
PostLogoutRedirectUri = PostLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
// REFERENCE: https://russellyoung.net/2015/09/05/mvc-role-based-authorization-with-azure-active-directory-aad/
AuthorizationCodeReceived = (AuthorizationCodeReceivedNotification notification) =>
{
var username = notification.AuthenticationTicket.Identity.Name.Split('#').LastOrDefault();
Logger.Log(Level.Auth, "Azure login success! Username: '" + username + "'.");
return Task.FromResult(0);
}
}
});
ARR网址重写设置
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="Secondary Server" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="https://qa2mobile.auntmillies.com/{R:1}" />
<serverVariables>
</serverVariables>
</rule>
</rules>
</rewrite>
问题
Azure AD身份验证正常工作正常,但是一旦我设置了反向代理,它就会开始将我带到我自己的域,就像它是身份提供者一样。
例如https://my.domain.com/<guid>/oauth2/authorize?client_id=<guid>&etc
my.domain.com
手动更改为login.microsoftonline.com
,它将使我登录并继续正常工作。login.microsoftonline.com
而不是我的域。是什么原因造成的?我的意思是,显然是反向代理,但是如何在不删除反向代理的情况下进行修复?
答案 0 :(得分:1)
是什么原因造成的?我的意思是,显然是反向代理,但是如何在不删除反向代理的情况下进行修复?
据我所知,如果您在web.config中使用url rewirte规则,它将把所有请求重写到https://my.domain.com/域中,其中包括login.microsoftonline.com。
我建议您可以编写一条新规则,该规则用于匹配/ oauth2请求url,以直接重新引导到login.microsoftonline.com而不是https://my.domain.com/。这样就可以了。
更多详细信息,您可以参考以下网址重写规则。
<rewrite>
<rules>
<rule name="rule2" stopProcessing="true">
<match url="<talentid>/oauth2/(.*)" />
<action type="Redirect" url="https://login.microsoftonline.com/{R:0}" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="https://<domain>.com/{R:1}" />
</rule>
</rules>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>