Microsoft Azure AD图API:如何检索用户的电子邮件地址?

bio*_*all 6 azure-active-directory azure-ad-graph-api microsoft-graph

我能够访问用户的accessToken,并GET https://graph.microsoft.com/v1.0/me使用Authorization: Bearer <token>标头进行调用.

但是,在响应正文中,我得到的是这样的:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
    "value": [
        {
            "givenName": "Foo",
            "surname": "Bar",
            "displayName": "Foo Bar",
            "id": "b41efha115adcca29",
            "userPrincipalName": "email.address@outlook.com",
            "businessPhones": [],
            "jobTitle": null,
            "mail": null,
            "mobilePhone": null,
            "officeLocation": null,
            "preferredLanguage": null
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

mail属性是null,并且userPrincipalName在此响应正文中恰好是用户的电子邮件地址.但是,这是来自微软的文档:

虽然UPN和电子邮件共享相同的格式,但用户的UPN值可能与用户的电子邮件地址相同,也可能不同.

在启动用户的登录请求时,我们正在请求"user.read""email"范围.我们使用MSAL.js库来获取访问令牌,我们的代码读取如下内容:

login (): ng.IPromise<IMicrosoftOAuthResponse> {
  const prom = this.userAgentApplication.loginPopup(["user.read", "email"])
    .then((idToken) => {
      return this.userAgentApplication.acquireTokenSilent(["user.read", "email"]);
    })
    .then((accessToken) => {
      // at this point, we have the accessToken and make a call to the graph api
    });
  return this.$q.when(prom);
}
Run Code Online (Sandbox Code Playgroud)

如何在此处获取用户的实际电子邮件地址?

Dan*_*SFT 5

mail属性设置为以下两种方式之一:

  1. 它已在本地 AD 上设置,然后使用 AD Connect 同步到 Azure AD
  2. 已为云用户分配了 Office 365 许可证(和邮箱),此时已mail为该许可用户设置了该属性。

如果用户没有 O365 邮箱/许可证,您还可以通过 userPrincipalName、displayName 等搜索用户。 $filter 支持 OR 运算符。

希望这可以帮助,