我有一个Web API和一个使用ADAL
库来调用Web API的UI应用程序。
我在向Azure AD注册应用时为Web API和UI应用提供了DELEGATED PERMISSIONS
(Read directory data
)。
我在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"
答案 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获取身份验证令牌。
希望这有帮助!