Microsoft Graph 的 MSAL 配置

MSAL Configuration for Microsoft Graph

我想使用 MSAL 获取 Graph API 的令牌,但为什么我的令牌受众总是指向我的客户端 ID,我是否需要更改流程以获取 MSAL 的令牌?当我尝试使用密码 grant_type 从邮​​递员那里获取令牌时,观众是 Microsoft Graph。 这是我的配置

export const authProvider = new MsalAuthProvider({
    auth: {
        authority: "https://login.microsoftonline.com/tenantId",
        clientId: "ClientId",
        postLogoutRedirectUri: window.location.origin,
        redirectUri: window.location.origin,
        validateAuthority: true,

        // After being redirected to the "redirectUri" page, should user
        // be redirected back to the Url where their login originated from?
        navigateToLoginRequestUrl: true
    },
    cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: false
    }
},
    {
        scope: ["https://graph.microsoft.com/.default", "user.read"],
        extraQueryParameters: { domain_hint: 'organizations' }
    },
    {
        loginType: LoginType.Redirect,

        tokenRefreshUri: window.location.origin + "/auth.html"
    },


)

这就是我获得令牌的方式

   const token = await authProvider.getIdToken();
        const idToken = token.idToken.rawIdToken;

这是获得 Microsoft Graph 的请求

我哪里错了?是我的配置还是我获取token的方式?

简单:这是因为您正在获取一个 ID Token,它不能用于访问受保护的资源(例如 MS Graph),因为:

  1. ID 令牌的“受众”(aud) 是请求它的客户端应用程序。
  2. ID 令牌没有范围 (scp) 声明,因此无法在 v2 endpoint.
  3. 中交换资源

您的配置至少还有 1 个问题:

{
    scope: ["https://graph.microsoft.com/.default", "user.read"],
    extraQueryParameters: { domain_hint: 'organizations' }
},

/.default scope 允许您一次性请求您在 Portal 上添加的所有静态权限。 /.default 作用域不能 not/should 与其他作用域组合,尤其是 v2 作用域,例如 user.read.

阅读有关如何使用 resources and scopes here 的更多信息(它适用于 MSAL.js,但适用相同的原则)。