Azure AD userAssertion:令牌缺少范围“Directory.Read.All”

时间:2018-03-25 11:49:37

标签: asp.net-web-api azure-active-directory

我有一个Web API和一个使用ADAL库来调用Web API的UI应用程序。

我在向Azure AD注册应用时为Web API和UI应用提供了DELEGATED PERMISSIONSRead directory data)。

enter image description here

我在Web API中有以下代码来为登录用户保存令牌,

private void ConfigureAuthentication(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
             new WindowsAzureActiveDirectoryBearerAuthenticationOptions
             {
                 Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                 TokenValidationParameters = new TokenValidationParameters { SaveSigninToken = true, ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] }
             });
    }

现在在Web API控制器中,我正在尝试使用以下代码获取令牌以访问Microsoft AD Graph API,

var bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as System.IdentityModel.Tokens.BootstrapContext;
            string userName = "test@onmicrosoft.com";
            string userAccessToken = bootstrapContext.Token;
            UserAssertion userAssertion = new UserAssertion(bootstrapContext.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);

            var authContext = new AuthenticationContext(string.Format(CultureInfo.InvariantCulture, aadInstance, tenant));
            var clientCred = new ClientCredential(clientId, appKey);

            var result = await authContext.AcquireTokenAsync("https://graph.windows.net", clientCred, userAssertion);
            accessToken = result.AccessToken;

上面的代码为我提供了令牌,但范围值低于,

`scp: "User.Read"`

问题 - 为什么令牌没有提供目录访问权限(Directory.Read.All),因为我已经设置了目录访问权限?

`scp: "Directory.Read.All User.Read"`

更新

我在DELEGATED PERMISSIONS下缺少读取目录数据的授予权限。提供Grant Permission后,我可以获得范围为scp: "Directory.Read.All User.Read"

的令牌

enter image description here

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望使用Microsoft Graph API,而不是Azure AD Graph API。

但是,根据您在此问题中发布的屏幕截图是v1 enpoint AAD应用程序,它对您尝试接近的Microsoft Graph API没有任何作用。因此,无论您在此应用程序上进行了哪些更改,结果都应该相同。我建议您在https://apps.dev.microsoft.com/

中注册v2 enpoint申请

以下是a document,其中显示了如何使用Microsoft Graph获取身份验证令牌。

希望这有帮助!