RequireNonce为'true'(默认值),但Azure Traffic Manager中的validationContext.Nonce为null,OpenIdConnectAuthentication

时间:2018-03-22 00:16:30

标签: azure-active-directory openid-connect azure-ad-graph-api arm-template azure-traffic-manager

我的网站(Azure App Service)部署在Microsoft Azure下的两个区域。 https://abcd1-westus.azurewebsites.net/
https://abcd2-centralus.azurewebsites.net/
我创建了一个流量管理器配置文件来控制服务端点的用户流量分配。流量管理员配置文件的DNS名称为“http://abcd.trafficmanager.net” 身份验证由Azure AD完成。 在我们尝试访问Traffic Manager DNS Url后,它会提示您进行AAD登录并重定向到https://abcd2.azurewebsites.net并出现黄页错误

“IDX10311:RequireNonce is'true' (默认值)但validationContext.Nonce为null。无法验证nonce。如果不需要检查nonce,请将OpenIdConnectProtocolValidator.RequireNonce设置为'false'。“

如果我打开个人网站网址,它可以完美运行。我在StartUp.Auth.cs中使用了以下代码。我正在使用 Microsoft.Owin.Security.OpenIdConnect ,版本3.1.0.0

public partial class Startup
{
    string secretKey = ConfigurationManager.AppSettings["AppKey"];
    string clientId = ConfigurationManager.AppSettings["ClientId"];
    string authority = ConfigurationManager.AppSettings["Authority"];
    string resource = ConfigurationManager.AppSettings["Resource"];
    string redirectUri = ConfigurationManager.AppSettings["RedirectUri"];
    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    RedirectToIdentityProvider = async n => {
                        n.ProtocolMessage.RedirectUri = n.OwinContext.Request.Uri.ToString();
                    },
                    AuthorizationCodeReceived = OnAuthorizationCodeReceived
                }
            });
    }

    private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
    {
        var code = context.Code;
        ClientCredential credential = new ClientCredential(clientId, secretKey);
        string userObjectID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
        AuthenticationContext authContext = new AuthenticationContext(authority, new NaiveSessionCache(userObjectID));
        Uri uri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
        AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(code, uri, credential, resource);
    }
}

请帮我解决此问题。生产接近:(

1 个答案:

答案 0 :(得分:0)

问题是您无法混合流量管理器URL和Web App URL。

nonce cookie在TM域上设置,重定向返回到不同的域。因此找不到nonce cookie。

因此,用户在地址栏中看到的URL应始终相同。如果他们通过https://abc.trafficmanager.net访问该网站,则Azure AD需要在身份验证后将其重定向到https://abc.trafficmanager.net。它不得在任何时候使用azurewebsites.net网址。