【发布时间】:2017-06-13 16:59:52
【问题描述】:
我编写了一个应该读取用户组的应用程序。我正在使用 Asp.net 核心。我在 azure 门户中创建了一个应用程序,并授予了 GraphAPI 的 all 应用程序权限,然后单击了授予权限按钮。然后我使用了一些类似于WebApp-GroupClaims-DotNet 的代码来检索用户组:
public async Task<IEnumerable<string>> GetGroupIdsFromGraphApiAsync(string userId)
{
var groupObjectIds = new List<string>();
// Acquire the Access Token
var credential = new ClientCredential(_configHelper.ClientId, _configHelper.AppKey);
var authContext = new AuthenticationContext(_configHelper.Authority);
var result = await authContext.AcquireTokenAsync(_configHelper.GraphResourceId, credential);
var accessToken = result.AccessToken;
var requestUrl =
$"{_configHelper.GraphResourceId}/{_configHelper.Domain}/users/{userId}/getMemberGroups?api-version=1.6";
// Prepare and Make the POST request
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = new StringContent("{\"securityEnabledOnly\":false}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content = content;
var response = await client.SendAsync(request);
// Endpoint returns JSON with an array of Group ObjectIDs
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var groupsResult = (Json.Decode(responseContent)).value;
foreach (string groupObjectId in groupsResult)
groupObjectIds.Add(groupObjectId);
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
throw new WebException(responseContent);
}
return groupObjectIds;
}
不幸的是,我总是得到以下回复:
{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"Insufficient privileges to complete the operation."}}}
应用程序是否无法向 AD 查询此信息?
【问题讨论】:
-
_configHelper.GraphResourceId的值是多少。似乎您正在使用 azure ad graph api,您是否在 azure 门户中授予 Windows Azure Active Directory(azure ad graph api) 的权限? -
_configHelper.GraphResourceId是https://graph.windows.net -
那么在 azure 门户中,您授予哪一个权限? Azure Active Directory 或 Microsoft Graph?以及哪个应用程序权限?
-
我添加了 Microsoft Graph 应用程序权限,但现在添加 Azure Active Directory 权限后它就可以工作了……这是为什么呢?我认为使用 graph.windows.net 会映射到 Microsoft Graph,而不是 AAD ...
-
请看我的回答。
标签: asp.net azure azure-active-directory microsoft-graph-api