我有两个可能相关的问题:
问题1:为什么用户的个人电子邮件地址会出现在Azure门户中作为用户主体名称?
问题2:如何通过用户的个人电子邮件地址查找用户?
我将要查找的电子邮件地址是用作登录名的电子邮件地址,因此我希望它应该出现在像signInNames
这样的属性中,如下所述。
复现步骤:
登录Azure门户,在任意用户处查看并观察其用户主体名称。请注意,它以个人电子邮件地址的格式显示(joe@somedomain.com)。复制该用户的对象ID。
在代码中,创建一个新的GraphServiceClient
,并使用上面步骤中复制的对象ID检索用户。
GraphServiceClient client = GetGraphServiceClient();
User user = await client.Users[userID].Request().GetAsync();
返回的用户对象中,需要注意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应用程序拥有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](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
,这里是我如何实现你的回答。在两种情况下,调用 graph 都返回用户,但用户的属性未被填充。我的应用程序具有 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];
}