Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 403使用图形API从Azure AD检索所有用户时禁止_C#_Azure Active Directory_Microsoft Graph Api - Fatal编程技术网

C# 403使用图形API从Azure AD检索所有用户时禁止

C# 403使用图形API从Azure AD检索所有用户时禁止,c#,azure-active-directory,microsoft-graph-api,C#,Azure Active Directory,Microsoft Graph Api,尝试使用Graph API获取所有用户时,我从Azure AD获得403禁止响应: public static async Task<string> AppAuthenticationAsync() { var tenant = ConfigurationManager.AppSettings["ida:TenantId"]; var resource = "https://graph.microsoft.com/";

尝试使用Graph API获取所有用户时,我从Azure AD获得403禁止响应:

    public static async Task<string> AppAuthenticationAsync()
    {
        var tenant = ConfigurationManager.AppSettings["ida:TenantId"];
        var resource = "https://graph.microsoft.com/";
        var clientID = ConfigurationManager.AppSettings["ida:ClientId"];
        var secret = ConfigurationManager.AppSettings["ida:AppKey"];

        var authority = $"https://login.microsoftonline.com/{tenant}";
        var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
        var credentials = new ClientCredential(clientID, secret);
        var authResult = await authContext.AcquireTokenAsync(resource, credentials);

        return authResult.AccessToken;
    }

    public static async Task<string[]> GetUsersListAsync(HttpClient client)
    {
        var payload = await client.GetStringAsync($"https://graph.microsoft.com/v1.0/users");

        var obj = JsonConvert.DeserializeObject<JObject>(payload);
        var users = from g in obj["value"]
                               select g["displayName"].Value<string>();

        return users.ToArray();
    }
在Azure AD中注册的客户端具有以下所有权限:


我遗漏了什么吗?

您已授予委派权限,但使用客户端凭据调用API

给你的应用程序在API上的权限,你就可以调用它了。
仅当您代表用户拨打电话时,授权权限才适用。

谢谢您的回复。但问题是,同一个web应用程序可以与另一个Azure AD应用程序配合使用,我只提供了委托权限。甚至委派的权限都是相同的。其他的东西一定不同,根本不可能将委派的作用域与客户端凭据OAuth Grant一起使用。@Marc谢谢你的建议,但我已经花了将近2-4个小时来验证这两个Azure AD应用程序,相信我,这两个应用程序的设置都是相同的。同时,让我尝试授予应用程序权限并检查发生了什么。设置相同,但应用程序中运行的代码相同吗?
   var token = await AppAuthenticationAsync();
   using (var client = new HttpClient())
   {
       client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
       var users = await GetUsersListAsync(client);
       lstADUsers = users.ToList();
   }