javascript - Microsoft Graph 的 MSAL 配置
问题描述
我想使用 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;
我错的部分在哪里?是我的配置还是我获取令牌的方式?
解决方案
很简单:这是因为您获得了一个ID Token,它不能用于访问受保护的资源(例如 MS Graph),因为:
- ID 令牌的“受众”(
aud
) 是请求它的客户端应用程序。 - ID 令牌没有范围 (
scp
) 声明,因此无法在v2 端点中交换资源。
您的配置至少还有 1 个问题:
{ scope: ["https://graph.microsoft.com/.default", "user.read"], extraQueryParameters: { domain_hint: 'organizations' } },
/.default范围允许您一次性请求在 Portal 上添加的所有静态权限。/.default
范围不能/不应与其他范围结合,尤其是v2
与user.read
.
在此处阅读有关如何使用资源和范围的更多信息(它适用于 MSAL.js,但适用相同的原则)。
推荐阅读
- python - flask - 在生产中访问 500 个内部服务器错误
- webpack - 如何让 Alias 在 Snowpack Svelte 应用程序中工作
- snowflake-cloud-data-platform - 在 Snowflake 中使用 ST_Intersects 与点和多面体有技巧吗?
- r - How to replace NAs in multiple columns with dplyr
- t4 - 当我升级到 .Net 4.8 时,T4 模板程序集加载停止运行
- java - 无法删除父实体,因为双向映射案例中的子引用
- python - Pyinstaller 在 onedir 模式下显示控制台
- javascript - 快速会话中间件中秘密的目的是什么?
- python - 如何在 PyCharm 中找到保存的文件
- typescript - 如何在 Typescript 中创建仅包含非空/未定义属性的对象?