0
votes

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 '[email protected]' 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["[email protected]"].Request().GetAsync();
1

1 Answers

2
votes

I can reproduce your issue. The account [email protected] 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();