首页 > 解决方案 > 如何使用 Azure AD 范围和 oidc-client.js 检索用户信息?

问题描述

在将 azure 广告范围与 oidc-client.js 一起使用时,我很困惑如何获取访问令牌和用户信息详细信息。

我在门户中针对我的应用程序具有以下范围...

在此处输入图像描述

然后我将我的用户管理器设置设置如下....

var settings: UserManagerSettings = {
    authority: `https://login.microsoftonline.com/${tenantId}`,
    client_id: clientId,
    redirect_uri: "http://localhost:3000/authcallback",
    post_logout_redirect_uri: "http://localhost:3000/authcallback",
    response_type: "token id_token",
    scope: `api://${clientId}/access_user_data openid`,
    popup_redirect_uri: "http://localhost:3000/authcallback",
    silent_redirect_uri: "http://localhost:3000/authcallback",
    automaticSilentRenew: true,
    loadUserInfo: true,
    metadata: {
        userinfo_endpoint: "https://graph.microsoft.com/oidc/userinfo",
        authorization_endpoint: `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/authorize`,
        issuer: `https://login.microsoftonline.com/${tenantId}/v2.0`,
        jwks_uri: `https://login.microsoftonline.com/${tenantId}/discovery/v2.0/keys`
    }
};

当我使用登录时,signinRedirect我会access_token返回到我的回调,但是https://graph.microsoft.com/oidc/userinfo在执行getUser().

oidc-client.min.js:1 GET https://graph.microsoft.com/oidc/userinfo 401 (Unauthorized)

访问令牌似乎确实适用于需要api://${clientId}/access_user_data范围的我的 api。

此处的发现文档列出了以下可用范围

"scopes_supported": [
    "openid",
    "profile",
    "email",
    "offline_access"
]

我认为这会起作用,因为我也包括openid范围。请注意,如果我只有这样的openid范围scope: "openid"getUser()则可以工作,但是它没有调用我的 api 所需的范围。

我在这里做错了什么?

谢谢,

标签: azure-active-directoryoidc-client-js

解决方案


在一些研究尝试将 loadUserInfo 设置为 false 解决了我的问题后遇到了同样的问题,请尝试相同的方法


推荐阅读