代码之家  ›  专栏  ›  技术社区  ›  dickiebow

使用电子邮件地址从Microsoft Graph API检索AD用户的入口ID?

  •  0
  • dickiebow  · 技术社区  · 1 年前

    在使用Microsoft Graph API通过将用户添加到特定组之前,我正在尝试从他们的电子邮件地址获取用户的Entra Id。Net Web应用程序。

    在Azure Portal中,我已经注册了应用程序并生成了一个客户端机密以使用客户端凭据Auth Flow。对于API权限,我已被授予用户权限。读取和用户。阅读一切都取决于我们公司的租户。

    我使用以下代码创建GraphServiceClient:

    public MicrosoftGraphService(IConfiguration configuration)
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };
    
        var tenantId = configuration["MicrosoftGraph:TenantId"];
        var clientId = configuration["MicrosoftGraph:ClientId"];
        var clientSecret = configuration["MicrosoftGraph:Secret"];
        var options = new ClientSecretCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };
    
        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    
        _graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);
    }
    

    我通过传入电子邮件地址来调用Graph API,试图检索用户对象,在那里我可以读取Entra ID。我使用以下代码来执行此操作:

    public async Task<GraphUser> GetUserAsync(string emailAddress)
    {
        try
        {
            var foundUser = await _graphServiceClient.Users[emailAddress].GetAsync();
            var user = (new GraphUser()
            {
                UserId = foundUser.Id,
                DisplayName = foundUser.DisplayName,
                Email = emailAddress
            });
    
            return user;
        }
        catch
        {
            // Not found in AD
            return null;
        }
    }
    

    但是,当它调用_graphServiceClient时。用户[电子邮件地址]。GetAsync()调用我得到一个错误:“权限不足,无法完成操作。”

    从文档中我已经阅读了拥有用户。阅读为应用程序授予的所有api权限都应该足以获取数据。我做错了什么?

    The permissions granted on the app registration

    1 回复  |  直到 1 年前
        1
  •  1
  •   Rukmini    1 年前

    错误 “权限不足,无法完成操作” 通常发生在Microsoft Entra ID应用程序没有执行操作所需的权限的情况下。

    最初我得到了 相同的错误 :

    enter image description here

    为了解决该错误, 确保给予 User.Read.All 应用程序类型 API权限,因为您正在使用客户端凭据流。

    • 使用者阅读全部的 使用用户交互流时,必须授予委托类型API权限。

    授予 使用者阅读全部的 应用程序类型 API权限如下:

    enter image description here

    我能够成功地检索到具有UPN的用户,如下所示:

    using Microsoft.Graph;
    using Azure.Identity;
    using Microsoft.Graph.Models.ODataErrors;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var clientId = "ClientID";
            var tenantId = "TenantID";
            var clientSecret = "ClientSecret";
    
            var options = new ClientSecretCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            };
    
            var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
            try
            {
                var result = await graphClient.Users["[email protected]"].GetAsync();
                Console.WriteLine($"User details: {result.DisplayName}, {result.Mail}, {result.Id}");
            }
                catch (ODataError odataError)
            {
                Console.WriteLine(odataError.Error?.Code);
                Console.WriteLine(odataError.Error?.Message);
                throw;
            }
        }
    }
    

    enter image description here

    参考

    Get a user - Microsoft Graph v1.0 | Microsoft