【问题标题】:Not able to get all users from Azure Active Directory无法从 Azure Active Directory 获取所有用户
【发布时间】:2023-01-26 14:19:08
【问题描述】:
我正在使用解决方案 mentioned here 从 Active Directory 中获取所有用户,但是我怀疑该代码正在从我们的旧 Active Directory 中提取禁用的用户。新的是 Azure Active Directory。请让我知道需要进行哪些更改才能从 Azure Active Directory 中获取以下仅活动用户的详细信息:
- 名字
- 姓氏
- 电邮
- 企业ID
【问题讨论】:
标签:
c#
soap
azure-active-directory
【解决方案1】:
获取 Azure AD 中的所有用户可以使用 Microsoft Graph API。这是 listing users 的 API。但是不支持personal微软账号,只支持work or school账号。顺便说一句,我不确定Enterprise ID是什么,你能看看this section看看这个API是否包含它吗?
我假设你有一个用于获取用户列表的 asp.net 核心 WEB API。所以你应该使用如下代码。
using Microsoft.Graph;
using Azure.Identity;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "aad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var users = await graphClient.Users.Request().GetAsync();
然后,上面代码中的变量需要一个 Azure AD 应用程序。请关注this document 注册 Azure AD 应用程序。由于我的假设是基于 Web API,因此无需在此处添加重定向 URL。现在我们可以在Overview刀片中获取tenantId , clientId,并创建客户端密钥。我们还需要修改API permissionsblade 并添加所需的API 权限。我们需要的是Application权限User.Read.All,User.ReadWrite.All,Directory.Read.All, Directory.ReadWrite.All。