c# - 如何使用 Microsoft Graph sdk 通过电子邮件地址从 Active Directory 获取用户信息

标签 c# azure azure-active-directory microsoft-graph-sdks

我们希望使用 Microsoft Graph SDK 从 Azure Active Directory 检索用户信息。

给出了有效的电子邮件地址,但我收到错误

Resource '[email protected]' does not exist or one of its queried reference-property objects are not present.

代码如下。可以指导一下吗?

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(clientId).WithTenantId(tenantID).WithClientSecret(clientSecret).Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var user = await graphClient.Users["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0bda9b5bdb1b9bcb1b4b4a2b5a3a390bcb9a6b5feb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>"].Request().GetAsync();

最佳答案

我可以重现您的问题。账号<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="375a4e525a565e5b56535345524444775b5e41521954585a" rel="noreferrer noopener nofollow">[email protected]</a>Guest在您的租户中,导航到门户中的 AAD -> 找到该帐户 -> 单击它并获取 Object ID ,然后使用Object ID在代码中,它会起作用。

var user = await graphClient.Users["<Object ID>"].Request().GetAsync();

或者您可以使用filter在您的情况下,让用户获得 UserPrincipalName 的格式对于 guest 用户将类似于 myemailaddress_live.com#EXT#@tenantname.onmicrosoft.com ,在使用过滤器时,我们需要对它进行URL编码,那么它将是 myemailaddress_live.com%23EXT%23%40tenantname.onmicrosoft.com ,尝试下面的代码,它在我这边有效。

var user = await graphClient.Users.Request().Filter("UserPrincipalName eq 'myemailaddress_live.com%23EXT%23%40tenantname.onmicrosoft.com'").GetAsync();

更新:

如果你想通过 UserPrincipalName 获取用户,您也可以使用如下的 url 编码。

var user = await graphClient.Users["myemailaddress_live.com%23EXT%23%40tenantname.onmicrosoft.com"].Request().GetAsync();

关于c# - 如何使用 Microsoft Graph sdk 通过电子邮件地址从 Active Directory 获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222315/

相关文章:

c# - 无法将 "obj\Debug\Program.exe"复制到 "bin\Debug\Program.exe"。超过重试次数 10。失败

azure-active-directory - MS Graph API - 受邀后立即对用户执行的某些操作失败

c# - 子属性更新调用它的父属性 `OnPropertyChanged`

c# - Xamarin.iOS iCloud 访问 iCloud 键值存储

git - 我应该在构建步骤中使用 `git push --force`

azure - Terraform:将敏感数据添加到 Azure KeyVault

azure - 无法使用不记名 token 访问 AAD 安全 Web API

angular - 获取 OAuth token 以供 Azure DevOps API 使用

c# - 使用 await Task.Run() 的异步方法从不 "completes"

azure - 将 mongorestore 恢复到 cosmosDB