How to get user's information from Active Directory by email address using Microsoft Graph sdk

孤者浪人 提交于 2019-12-11 16:30:16

问题


We would like to retrieve user's information from Azure Active directory using Microsoft Graph SDK.

Given a valid email address, but I get an error

Resource 'myemailaddress@live.com' does not exist or one of its queried reference-property objects are not present.

Code is below. Can you please guide?

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["myemailaddress@live.com"].Request().GetAsync();

回答1:


I can reproduce your issue. The account myemailaddress@live.com is a Guest in your tenant, navigate to the AAD in the portal -> find the account -> click it and fetch the Object ID, then use the Object ID in the code, it will work.

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

Or you can use filter to get the user, in your case, the format of the UserPrincipalName for the guest user will be like myemailaddress_live.com#EXT#@tenantname.onmicrosoft.com, when using the filter, we need URL encode it, then it will be myemailaddress_live.com%23EXT%23%40tenantname.onmicrosoft.com, try the code as below, it works on my side.

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

Update:

If you want to get the user via UserPrincipalName, you can also use the url encoded one as below.

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


来源:https://stackoverflow.com/questions/58222315/how-to-get-users-information-from-active-directory-by-email-address-using-micro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!