简体   繁体   English

无法从 Microsoft 图形 API 获取 Azure AD 中存在的用户

[英]Cannot fetch users present in Azure AD from Microsoft graph API

I have made a MVC application and I'm trying to add users to application.我制作了一个 MVC 应用程序,我正在尝试将用户添加到应用程序中。

The validation of user needs to be done through Azure AD for which Graph API is used.用户验证需要通过 Azure AD 完成,其中使用了图 API。

Internal users presented in Azure AD are added successfully. Azure AD 中显示的内部用户已成功添加。

External users are also presented in Azure AD but I get the error外部用户也出现在 Azure AD 但我收到错误

User not present in AD用户不在 AD 中

I have tried applying all filters but still getting the same issue.我尝试应用所有过滤器,但仍然遇到同样的问题。

Code used:使用的代码:

var authProvider = await GetAuthProvider();
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

var user = await graphClient.Users[email].Request().GetAsync();
return true;

where email is the string of email passed from application.其中 email 是从应用程序传递的 email 的字符串。 The code works fine for internal users but not for external users.该代码适用于内部用户但不适用于外部用户。

The issue is that you are using mail property but you should use userPrincipalName问题是您正在使用mail属性,但您应该使用userPrincipalName

mail has form like this mail有这样的形式

AdeleVance@adatum.com

but userPrincipalName for external user has different form但是外部用户的userPrincipalName有不同的形式

AdeleVance_adatum.com#EXT#@contoso.com
AdeleVance_adatum.com#EXT#@contoso.onmicrosoft.com

To query a external user using the userPrincipalName , encode the hash (#) character.要使用userPrincipalName查询外部用户,请对 hash (#) 字符进行编码。 Replace the # symbol with %23.将 # 符号替换为 %23。

AdeleVance_adatum.com%23EXT%23@contoso.com

But graphClient should encode the hash character automatically.但是graphClient应该自动编码 hash 字符。

Resources:资源:

Get a user: HTTP request 获取用户:HTTP请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM