猿问

如何从 Microsoft Graph API C# 中的电子邮件地址获取用户 ID

我想将用户添加到组中,但我没有用户的id,我只有电子邮件地址。


这是代码:


User userToAdd = await graphClient

    .Users["objectID"]

    .Request()

    .GetAsync();


await graphClient

    .Groups["groupObjectID"]

    .Members

    .References

    .Request()

    .AddAsync(userToAdd);

有人可以帮助我如何使用 Microsoft Graph 从电子邮件地址检索 ObjectId(用户 ID)?


ABOUTYOU
浏览 367回答 3
3回答

HUWWW

您可以通过几种不同的方式查找用户。从/users端点,您可以使用他们的id(分配给每个帐户的 GUID)或他们的userPrincipalName(默认域的电子邮件别名):// Retrieve a user by idvar user  = await graphClient    .Users["00000000-0000-0000-0000-000000000000"]    .Request()    .GetAsync();// Retrieve a user by userPrincipalNamevar user  = await graphClient    .Users["user@tenant.onmicrosoft.com"]    .Request()    .GetAsync();如果您使用授权代码或隐式OAuth 授权,您还可以查找通过/me端点进行身份验证的用户:var user = await graphClient    .Me    .Request()    .GetAsync();

蝴蝶刀刀

除了上面的这些正确答案。userPrincipalName或者id不是外部电子邮件地址。这就是我手动完成注册时的情况。您可以使用此过滤器:var user = await _graphClient.Users&nbsp; &nbsp; .Request()&nbsp; &nbsp; .Filter($"identities/any(c:c/issuerAssignedId eq '{email}' and c/issuer eq 'contoso.onmicrosoft.com')")&nbsp; &nbsp; .GetAsync();源代码:https : //docs.microsoft.com/en-us/graph/api/user-list? view = graph-rest-1.0 & tabs =csharp使用此配置创建用户:Microsoft.Graph.User graphUser = new Microsoft.Graph.User{&nbsp; &nbsp; AccountEnabled = true,&nbsp; &nbsp; PasswordPolicies = "DisablePasswordExpiration,DisableStrongPassword",&nbsp; &nbsp; PasswordProfile = new PasswordProfile&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForceChangePasswordNextSignIn = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password = accountModel.Password,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; Identities = new List<ObjectIdentity>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ObjectIdentity()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SignInType = "emailAddress",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Issuer ="contoso.onmicrosoft.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IssuerAssignedId = email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },};

白衣非少年

您可以使用id或userPrincipalName(这是一个电子邮件地址)从 Graph API 检索用户的详细信息。来自Microsoft Graph API 参考:GET&nbsp;/users/{id&nbsp;|&nbsp;userPrincipalName}您是否尝试将电子邮件地址用作objectID?
随时随地看视频慕课网APP
我要回答