如何使用 Graph API 在 B2C 上访问用户的电子邮件

How to access Email of an User on B2C using Graph API

我想知道发送电子邮件的用户的电子邮件地址。 在我的应用程序中,人们可以使用社交帐户 (google/facebook/Microsoft) 或本地帐户注册。 创建本地帐户时,我们使用电子邮件。

我找到了有关电子邮件存储方式的信息。 https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-policies

Email address storage: An email address can be required as part of a user flow. If the user authenticates with a social identity provider, the email address is stored in the otherMails property. If a local account is based on a user name, then the email address is stored in a strong authentication detail property. If a local account is based on an email address, then the email address is stored in the signInNames property. The email address isn't guaranteed to be verified in any of these cases. A tenant administrator can disable email verification in the basic policies for local accounts. Even if email address verification is enabled, addresses aren't verified if they come from a social identity provider and they haven't been changed. Only the otherMails and signInNames properties are exposed through the Active Directory Graph API. The email address in the strong authentication detail property is not available

不确定为什么未使用用户的字段 "Mail"...但使用 GraphApi:

我做了一个 GET:https://graph.microsoft.com/v1.0/Users?$select=displayName,mail,otherMails,signInNames

有些电子邮件出现在 "mail" 上,其他电子邮件出现在 "otherMails" 的数组中,并且 "singInNames" 无法 selected :( 不显示任何信息,有些用户也是如此,我无法获取有关电子邮件的信息。

我该如何解决这个问题?仅使用 Azure AD Graph 而不是 Microsoft Graph API,因为返回 API signInNames?

没有任何方法可以将电子邮件始终存储在同一个 属性 上吗?或者至少有一个我可以在 Microsoft Graph API 上访问?仅将自定义策略用于声明转换?

您需要从不同的地方收集电子邮件地址,例如 mailotherMailssignInNames 通过 AD Graph API。 signInNames NOT 在 Microsoft Graph API.

中可用

请注意,在用户使用用户名+电子邮件验证登录的情况下,无法检索使用的电子邮件。

或者您可以添加 custom attribute in custom policy, where you can require users to type in their email address. After that, you could use AD Graph to get the custom attribute (A sample here)。

我通过以下 Microsoft Graph API 调用成功获取了 Azure B2C 用户的电子邮件:

https://graph.microsoft.com/v1.0/users?$select=identities

在 Microsoft Graph API 中,您可以使用:

GET: https://graph.microsoft.com/v1.0/Users?$select=displayName,mail,identities,otherMails

您可以在 identities 集合中找到本地帐户的电子邮件。

在 Graph 的 BETA 版本中 API (graph.microsoft.com/beta) identities 和 otherMails 属性也会在没有 $select 的情况下返回,在 v1 中.0 版本仅当在 $select.

中指定时