首页 > 解决方案 > 从 Azure Active Directory 访问具有授权类型的用户

问题描述

我是 Azure 的新手。我需要从 Azure 活动目录中检索用户。我有委托类型、user.Read 和 users.Readall。

我能够获得承载代码,但是在检索用户时我被拒绝了权限。我需要用 c# 编写一个控制台应用程序,下面是我尝试过的代码。

var app = ConfidentialClientApplicationBuilder.Create(clientID)
               .WithClientSecret(Secret)
               .WithRedirectUri(URL)
               .WithAuthority("https://login.microsoftonline.com/tenantID/oauth2/token")
               .Build();

            string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
            var token = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;

 - GraphServiceClient graphClient1 = new
   GraphServiceClient("https://graph.microsoft.com/v1.0", new
   DelegateAuthenticationProvider(async (requestMessage) =>
               {
                   requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
               }));
   
               var users = graphClient1.Users.Request().Top(99).GetAsync();
               var organization = graphClient1.Organization.Request().GetAsync().Result;

我在下面

system.AggregateException: 'One or more errors occurred. (Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
for the LIne **var organization = graphClient1.Organization.Request().GetAsync().Result;**
Need help on this

提前致谢。

标签: c#azureazure-active-directory

解决方案


由于您使用的是委托权限,因此需要用户登录才能使用这些权限。因此,这https://graph.microsoft.com/.default不是正确的范围,它应该具有委托权限,例如https://graph.microsoft.com/User.Read.All.

此外,您需要在图形请求之前使用 await,

var users = await graphClient.Users.Request().Top(99).GetAsync();

由于您是 Ms Graph 的新手,请通过一个简单的控制台应用程序代码示例帮助您轻松实现。


推荐阅读