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



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

问题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-9ac960a6bdab@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

根据这个答案的建议,这也不起作用:

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

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

编辑

为了回应这里发布的答案,我尝试了这个代码:

// 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://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=CS)

以下是我如何使用GraphServiceClient实现您的答案。在这两种情况下,对图的调用都会返回用户,但不会填充用户的属性。我的应用程序具有"用户"权限。ReadWrite。全部,用户。管理身份。全部,域。ReadWrite。全部的这是权限问题吗?

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];
}

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

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

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

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

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

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

这些解决方案对我都不起作用。不过,您必须具有正确的权限。

我正在使用https://graph.microsoft.com/v1.0作为端点。

var officeUser = await graph.Users
.Request()
.Filter($"mail eq '{emailAddress}'")
.Select(e => new {
e.DisplayName,
e.Mail,
e.Id
})
.GetAsync();

你能试试这些图形调用吗:

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

在使用全局管理员或用户管理员帐户登录后,单击左侧窗格上的"登录到图形资源管理器"按钮,通过图形资源管理程序。此外,请确保您正在使用的用户帐户是B2C租户中的成员帐户(通过Azure门户网站>Azure Active Directory>用户>新用户>创建用户选项创建(,而不是访客或注册用户。

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

在搜索有效答案的几天里,我在Graph Explorer中尝试了多种方法,发现对我的场景有效的是

这将把用户放在一个列表中,形成逗号分隔的字符串,并将其用作筛选器,或者如果您只想让一个用户使用eq运算符。

var userBatchStr = "'user1@domain.com', 'user2@domain.com'";
// Get Multiple Users
var users = this.graphServiceClient.Users
.Request()
.Filter($"userPrincipalName in ({userBatchStr})")
.GetAsync()
.Result;
// Get Single User
var user = this.graphServiceClient.Users
.Request()
.Filter($"userPrincipalName eq 'user1@domain.com'")
.GetAsync()
.Result;

您可以使用此API获取信息

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

最新更新