图API不断returnsAuthorization_RequestDenied虽然权限被授予
Graph API constantly returns Authorization_RequestDenied although permissions are granted
我们正在尝试使用 Graph API 来设置由 Azure AD 管理的用户帐户的属性。这个想法是,在用户完成注册策略后,我们的后端 Web API 被调用(由 Azure AD)来设置一些用户不能自己设置的属性(例如,新用户的内部客户 ID ).
不幸的是,Graph API 在尝试发送更新用户的补丁请求时不断 returns 异常 ("Insufficient privileges tom compete this operation")。
我正在尝试以管理员身份授予所有用户权限,因为更新帐户时没有用户登录。我基本上希望我的应用程序成为能够自动管理用户配置文件的第三方。
我的代码如下所示:
public class GraphAPIClient
{
private AuthenticationContext authContext;
private ClientCredential clientCredential;
private string adTenant;
public GraphAPIClient(string tenant, string clientID, string clientSecret)
{
authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
clientCredential = new ClientCredential(clientID, clientSecret);
adTenant = tenant;
}
public async Task<string> UpdateUserInfo(string objectID, string jsonUpdate)
{
return await SendPatchRequest("/users/" + objectID, jsonUpdate);
}
private async Task<string> SendPatchRequest(string api, string jsonRequest)
{
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net/", clientCredential);
HttpClient httpClient = new HttpClient();
string url = "https://graph.windows.net/" + adTenant + api + "?" + "api-version=1.6";
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
request.Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if(!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formattedError = JsonConvert.DeserializeObject(error);
throw new Exception("Graph API request failed:\n" + JsonConvert.SerializeObject(formattedError, Formatting.Indented));
}
return await response.Content.ReadAsStringAsync();
}
}
我使用已注册应用程序的应用程序 ID、目录的租户和我在 Azure 中生成的密钥作为客户端机密。我成功收到令牌了。
我尝试过的:
- 我已经设置(或者我认为我拥有)编辑用户所需的权限。我使用管理员帐户单击 "Grant Permissions",在 Azure 面板中查看时会显示权限:
Image
- 我设置了 "Read and write all users' full profiles" 和 "Read and write directory data"。这些是执行此类任务的正确权限吗?
- 我也尝试过使用新的应用程序注册控制台,但是在尝试获取令牌时出现 "The identity of the calling application could not be established" 异常。在此控制台中创建的应用程序在普通 Azure AD 面板中不可见。
谁能帮我解决这个问题?我已经彻底搜索过,但大多数关于这个问题的问题都比较老,建议使用旧的 Azure 面板。我已经尝试过了,但它仍然不起作用,我认为新的 Azure 面板最近才收到 AD 编辑功能,而在提出这些问题时却没有。
非常感谢 - 如果您需要更多信息,我很乐意提供。
您的场景和代码都反映了 客户端凭据流和应用程序权限 的使用。但是,您从 Azure 门户截取的屏幕截图仅显示委派权限设置。
根据您想要的 permissions/operations 类型,您可以利用两种方法:
设置最简单但权限集有限:通过 Azure 门户。确保您从 "Application Permissions" 获得 select 权限然后点击 授予权限 。如果 none 出现,那是因为您已将您的应用程序创建为本机应用程序。对于将安全地位于您的服务器端(不交给最终用户)的无头客户端,您应该选择 Web 应用程序/API,这将使您能够访问应用程序权限:
最难设置但权限范围最广:通过 PowerShell。 如果您希望能够删除用户,这是唯一可行的选择。您需要获取应用程序的 ServicePrincipalID,然后按如下方式授予服务主体所需的角色:
$appDisplayName = <Your-app-display-name>
$msolcred = Get-Credential
Connect-MsolService -credential $msolcred
$servicePrincipal = Get-MsolServicePrincipal | where DisplayName -eq $appDisplayName
$objectId = $servicePrincipal.ObjectId.ToString()
Add-MsolRoleMember -RoleObjectId 88d8e3e3-8f55-4a1e-953a-9b9898b8876b -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId 9360feb5-f418-4baa-8175-e2a00bac4301 -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId fe930be7-5e62-47db-91af-98c3a49a38b1 -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal
或者,您可以按照 this article. 中的步骤操作。
我们正在尝试使用 Graph API 来设置由 Azure AD 管理的用户帐户的属性。这个想法是,在用户完成注册策略后,我们的后端 Web API 被调用(由 Azure AD)来设置一些用户不能自己设置的属性(例如,新用户的内部客户 ID ).
不幸的是,Graph API 在尝试发送更新用户的补丁请求时不断 returns 异常 ("Insufficient privileges tom compete this operation")。 我正在尝试以管理员身份授予所有用户权限,因为更新帐户时没有用户登录。我基本上希望我的应用程序成为能够自动管理用户配置文件的第三方。
我的代码如下所示:
public class GraphAPIClient
{
private AuthenticationContext authContext;
private ClientCredential clientCredential;
private string adTenant;
public GraphAPIClient(string tenant, string clientID, string clientSecret)
{
authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
clientCredential = new ClientCredential(clientID, clientSecret);
adTenant = tenant;
}
public async Task<string> UpdateUserInfo(string objectID, string jsonUpdate)
{
return await SendPatchRequest("/users/" + objectID, jsonUpdate);
}
private async Task<string> SendPatchRequest(string api, string jsonRequest)
{
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net/", clientCredential);
HttpClient httpClient = new HttpClient();
string url = "https://graph.windows.net/" + adTenant + api + "?" + "api-version=1.6";
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
request.Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if(!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formattedError = JsonConvert.DeserializeObject(error);
throw new Exception("Graph API request failed:\n" + JsonConvert.SerializeObject(formattedError, Formatting.Indented));
}
return await response.Content.ReadAsStringAsync();
}
}
我使用已注册应用程序的应用程序 ID、目录的租户和我在 Azure 中生成的密钥作为客户端机密。我成功收到令牌了。
我尝试过的:
- 我已经设置(或者我认为我拥有)编辑用户所需的权限。我使用管理员帐户单击 "Grant Permissions",在 Azure 面板中查看时会显示权限: Image
- 我设置了 "Read and write all users' full profiles" 和 "Read and write directory data"。这些是执行此类任务的正确权限吗?
- 我也尝试过使用新的应用程序注册控制台,但是在尝试获取令牌时出现 "The identity of the calling application could not be established" 异常。在此控制台中创建的应用程序在普通 Azure AD 面板中不可见。
谁能帮我解决这个问题?我已经彻底搜索过,但大多数关于这个问题的问题都比较老,建议使用旧的 Azure 面板。我已经尝试过了,但它仍然不起作用,我认为新的 Azure 面板最近才收到 AD 编辑功能,而在提出这些问题时却没有。
非常感谢 - 如果您需要更多信息,我很乐意提供。
您的场景和代码都反映了 客户端凭据流和应用程序权限 的使用。但是,您从 Azure 门户截取的屏幕截图仅显示委派权限设置。
根据您想要的 permissions/operations 类型,您可以利用两种方法:
设置最简单但权限集有限:通过 Azure 门户。确保您从 "Application Permissions" 获得 select 权限然后点击 授予权限 。如果 none 出现,那是因为您已将您的应用程序创建为本机应用程序。对于将安全地位于您的服务器端(不交给最终用户)的无头客户端,您应该选择 Web 应用程序/API,这将使您能够访问应用程序权限:
最难设置但权限范围最广:通过 PowerShell。 如果您希望能够删除用户,这是唯一可行的选择。您需要获取应用程序的 ServicePrincipalID,然后按如下方式授予服务主体所需的角色:
$appDisplayName = <Your-app-display-name> $msolcred = Get-Credential Connect-MsolService -credential $msolcred $servicePrincipal = Get-MsolServicePrincipal | where DisplayName -eq $appDisplayName $objectId = $servicePrincipal.ObjectId.ToString() Add-MsolRoleMember -RoleObjectId 88d8e3e3-8f55-4a1e-953a-9b9898b8876b -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal Add-MsolRoleMember -RoleObjectId 9360feb5-f418-4baa-8175-e2a00bac4301 -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal Add-MsolRoleMember -RoleObjectId fe930be7-5e62-47db-91af-98c3a49a38b1 -RoleMemberObjectId $objectId -RoleMemberType servicePrincipal
或者,您可以按照 this article. 中的步骤操作。