当尝试使用访问图形服务客户端时,我收到错误:
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
研究此错误后,最常见的解决方案是设置 API 的权限。这已经完成并且具有读取基本/完整配置文件的权限。
我已删除并重新添加了 API。
下面是我的 AzureAuthenticationProvider
类中的代码,它继承自 IAuthenticationProvider
:
public class AzureAuthenticationProvider : IAuthenticationProvider
{
private string _azureDomain = "myDevDom.onmicrosoft.com";
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
try
{
string clientId = "2b823c67-1b0d-4a10-a9e1-737142516f5q";
string clientSecret = "xxxxxx";
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/" + _azureDomain + "/oauth2/token");
ClientCredential credentials = new ClientCredential(clientId, clientSecret);
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);
request.Headers.Add("Authorization", "Bearer " + authResult.AccessToken);
}
catch (Exception ex)
{
}
}
}
我尝试将客户端 key 更改为无效 ID,但它引发了错误,因此客户端 key 是正确的。我还尝试通过更改访问 token 来验证访问 token 是否有效,这也会返回错误。
上面的代码似乎工作正常。
下面是我尝试访问 Azure AD 的代码:
public async Task<IGraphServiceUsersCollectionPage> GetUsersByLastName(string lastname)
{
GraphServiceClient graphClient = new GraphServiceClient(new AzureAuthenticationProvider());
string filter = String.Format("startswith(surname, '{0}')", lastname);
IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().Filter(filter).GetAsync(); //Fails on this line
return users;
}
最佳答案
请引用以下步骤:
从您的屏幕截图来看,您似乎授予了
Windows Azure Active Directory
(azure ad graph api) 的读取和写入目录数据
应用程序权限。由于您使用的是 microsoft graph ( https://graph.microsoft.com/ ),因此您需要授予Microsoft Graph
的应用程序权限:由于您是 AAD 的管理员,因此您可以通过点击上面屏幕截图中显示的
授予权限
按钮向组织中的用户授予权限。然后您可以使用您的代码(获取 token 的客户端凭据流)并查询用户信息。如果您检查 azure ad 颁发的访问 token 中的声明,您可以在
roles
声明中找到Directory.Read.All
权限。
关于c# - 图形 API - 权限不足,无法完成操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44323560/