Microsoft Graph:如何通过个人电子邮件地址查找用户?

时间:2020-08-05 23:51:00

标签: azure microsoft-graph-api

我实际上有两个可能与之相关的问题:

问题1:为什么用户个人电子邮件地址作为用户主体名称出现在Azure门户中?

问题2:如何通过用户的个人电子邮件地址查找用户?

我要查找的电子邮件地址是用作登录名称的电子邮件地址,因此我希望它应该出现在signInNames之类的属性中,如下所述。

复制步骤:

登录到Azure门户,查看一个随机用户并观察其用户主体名称。 请注意,它以个人电子邮件地址(joe@somedomain.com)的形式出现。复制用户的对象ID。

在代码中,创建一个新的GraphServiceClient并使用上一步中复制的对象ID通过对象ID检索用户。

GraphServiceClient client = GetGraphServiceClient();
User user = await client.Users[userID].Request().GetAsync();

在返回的User对象中,请注意UserPrincipalName的值不是第一步中所述的Azure门户中显示的值。而是分配了一个标识符:cpim_96fe-48b5-88a2-9ac960a​​6bdab@mydomain.onmicrosoft.com。

尝试使用个人电子邮件地址See also查找用户:

GraphServiceClient client = GetGraphServiceClient();
IGraphServiceUsersCollectionPage users = await client.Users.Request().Filter("userPrincipalName eq 'joe@somedomain.com'").GetAsync(); // Count = 0
IGraphServiceUsersCollectionPage users = await client.Users.Request().Filter("mail eq 'joe@somedomain.com'").GetAsync(); // Count = 0

根据this answer的建议,这也不起作用:

IGraphServiceUsersCollectionPage users3 = await client.Users.Request().Filter("signInNames/any(x:x/value eq 'joe@somedomain.com')").GetAsync(); // error Filter not supported.

我的Azure应用程序具有User.ReadWrite.All权限。个人电子邮件地址未显示为我检索的任何对象的任何属性值。

编辑

针对发布的答案here,我尝试了以下代码:

// Exact call to graph:
    https://graph.microsoft.com/beta/users?$filter=otherMails/any(id:id%20eq%20'my.name@outlook.com')
    
    Error message:
    
    System.NotSupportedException : The collection type 'Microsoft.Graph.IUserAppRoleAssignmentsCollectionPage' on 'Microsoft.Graph.User.AppRoleAssignments' is not supported.
    
    [Question regarding above error](https://stackoverflow.com/questions/62776361/how-to-use-graph-explorer-sdk)
    
    [Instruction to use GraphClient](https://docs.microsoft.com/en-us/graph/sdks/create-client?tabs=CS)

我使用GraphServiceClient来实现您的答案。在这两种情况下,对图形的调用都会返回用户,但是不会填充用户的属性。我的应用程序具有User.ReadWrite.All,User.ManageIdentities.All,Domain.ReadWrite.All权限。这是权限问题吗?

    public async Task<User> GetUserByEmailAddress2(string email)
    {
        GraphServiceClient client = GetGraphServiceClient();
        IGraphServiceUsersCollectionPage users = await client.Users.Request().Filter($"otherMails/any(id:id eq '{email}')").GetAsync();
        var nonnullusers = users.Where(x => x.OtherMails?.Any() ?? false).ToList(); // count = 0
        
        users = await client.Users.Request().Filter($"identities/any(id:id/issuer eq 'LeaderAnalytics.onmicrosoft.com' and id/issuerAssignedId eq '{email}')").GetAsync();
        nonnullusers = users.Where(x => x.Id == email).ToList(); // count = 0

        return users[0];
    }

4 个答案:

答案 0 :(得分:0)

请您尝试以下图形调用:

https://graph.microsoft.com/beta/users?$filter=otherMails/any(id:id eq'user@example.com') https://graph.microsoft.com/beta/users?$filter=identities/any(id:id/issuer eq'xxxx.onmicrosoft.com'和id / issuerAssignedId eq'user@example.com')

通过单击左侧窗格上的“登录到图形资源管理器”按钮,使用全局管理员或用户管理员帐户登录后,通过Graph Explorer通过

。此外,还要确保您使用的用户帐户是B2C租户中的成员帐户(通过Azure门户> Azure Active Directory>用户>新用户>创建用户选项创建),并且不是访客或注册用户。

请告诉我这是否可行,以及您是否在代码中以相同用户的上下文进行这些调用。

答案 1 :(得分:0)

要在电子邮件地址填充为备用电子邮件(otherMails属性)时搜索用户:

  • graph.microsoft.com/beta/users?$filter=otherMails/any(id:id eq'user@example.com')

要在电子邮件地址填充为登录名(signInNames属性)时搜索用户:

  • graph.microsoft.com/beta/users?$filter=identities/any(id:id / issuer eq'xxxx.onmicrosoft.com'和id / issuerAssignedId eq'user@example.com')

也请接受答案here

答案 2 :(得分:0)

使用 C# SDK 时,您的调用将是:

var users = await _graphClient.Users.Request()
                .Filter($"identities/any(id:id/issuer eq 'xxx.onmicrosoft.com' and id/issuerAssignedId eq '{emailAddress}')")
                .GetAsync();

答案 3 :(得分:-1)

您可以使用此API获取信息

获取https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}?$ select = displayName,givenName,postalCode