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

5 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();
Run Code Online (Sandbox Code Playgroud)

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

尝试使用个人电子邮件地址查找用户另请参阅

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
Run Code Online (Sandbox Code Playgroud)

正如这个答案所建议的,这也不起作用:

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

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

编辑

为了响应此处发布的答案,我尝试了以下代码:

// 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](/sf/ask/4394345301/)
    
    [Instruction to use GraphClient](https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=CS)
Run Code Online (Sandbox Code Playgroud)

使用GraphServiceClient here是我实现你的答案的方式。在这两种情况下,对图表的调用都会返回用户,但不会填充用户的属性。我的应用程序具有权限 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];
    }
Run Code Online (Sandbox Code Playgroud)

Ama*_*sft 10

当电子邮件地址填充为备用电子邮件(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')


Sve*_*ven 7

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

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