c# - 图形 API - 权限不足,无法完成操作

标签 c# azure permissions azure-active-directory azure-ad-graph-api

当尝试使用访问图形服务客户端时,我收到错误:

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;
}

azureADpermissionProperties

最佳答案

请引用以下步骤:

  1. 从您的屏幕截图来看,您似乎授予了 Windows Azure Active Directory(azure ad graph api) 的读取和写入目录数据应用程序权限。由于您使用的是 microsoft graph ( https://graph.microsoft.com/ ),因此您需要授予 Microsoft Graph 的应用程序权限: enter image description here

  2. 由于您是 AAD 的管理员,因此您可以通过点击上面屏幕截图中显示的授予权限按钮向组织中的用户授予权限。

  3. 然后您可以使用您的代码(获取 token 的客户端凭据流)并查询用户信息。如果您检查 azure ad 颁发的访问 token 中的声明,您可以在 roles 声明中找到 Directory.Read.All 权限。

关于c# - 图形 API - 权限不足,无法完成操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44323560/

相关文章:

java - 如何授予新创建的角色加入新创建的 JDA 库语音聊天的权限?

python - IO错误: [Errno 13] Permission denied: I do have permissions?

file - gcloud compute scp - 权限被拒绝错误

C# 表达式比较

azure - 通过 PowerShell 获取 Azure 目录名称

azure - 使用 ADOMD.NET 连接到 Azure Analysis 时出现连接字符串错误

Azure B2C 无密码注册,仅使用自定义策略中的电子邮件

c# - 委托(delegate)添加和删除

c# - 无法使用 EF 交换两行上的唯一值

c# - Invalidate()将东西留在屏幕上