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

前端之家收集整理的这篇文章主要介绍了azure-active-directory – Microsoft Azure AD图API:如何检索用户的电子邮件地址?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我能够访问用户的accessToken,并且正在使用Authorization:Bearer< token>来调用GET https://graph.microsoft.com/v1.0/me.头.

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

{
    "@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
        }
    ]
}

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

Although the UPN and email share the same format,the value of the UPN for a user might or might not be the same as the email address of the user.

在启动用户登录请求时,我们正在请求“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);
}

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

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

>它已在内部部署AD上设置,然后使用AD Connect同步到Azure AD
>已为云用户分配了Office 365许可证(和邮箱),此时为此许可用户设置了邮件属性.

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

希望这可以帮助,

猜你在找的Windows相关文章