MSAL acquireTokenSilent 每次都会发出新请求,缓存被忽略

问题描述 投票:0回答:1

@azure/msal-浏览器 3.20.0 @azure/msal-react:2.0.22

我有 SPA React 客户端应用程序和后端 API。 我注册了两个应用程序,公开了自定义 api,并以管理员身份同意。
一切正常,我成功从受保护的 API 收到 200 ok 消息。
在某些时候,我注意到调用

instance.acquireTokenSilent
会导致每次都向 MS 发出请求,而它应该使用存储在缓存中的令牌。
在这些冗余调用中收到的令牌显然与已存储在缓存中的令牌相同,因为该令牌仍然有效 这是我的自定义范围

export const loginRequest = {
    scopes: ["api://my-guid/tasks.read"]
    forceRefresh: false
};

查看缓存,范围保存在属性

target
中,并且值是相同的

api://my-guid/tasks.read

.
但我注意到的是,当我手动解密访问令牌时,除了其他声明之外,我得到

"scp": "tasks.read"

它被剥夺了

api:://guid/
部分,我不知道这是否是原因,但我至少觉得它很可疑。

我发现了几个类似的主题,例如这个,但这是 b2c 的一个已知问题,另外解决方案是使用我已经使用的自定义范围。
其他解决方案建议分别为每个作用域调用 acquireTokenSilent,但我只使用单个作用域,因此这也不应该成为问题。

msalConfig

export const msalConfig = {
    auth: {
        clientId: "client-guid",
        authority: "https://login.microsoftonline.com/tenant-guid",
        redirectUri: "https://localhost:5173/",
    },
    cache: {
        cacheLocation: "sessionStorage", // This configures where your cache will be stored
        storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge     
    },
}

asp.net-core-webapi microsoft-entra-id minimal-apis msal-react
1个回答
0
投票

经过进一步调查,我发现我正在使用不正确的参数调用 acquireTokenSilent 。我实际上是用两个道具传递物体。 request prop 持有带有scopes prop 的对象

  • 请求:对象
  • 账号:账号

上次通话

const tokenResponse = await instance.acquireTokenSilent({ request: accessTokenRequest, account: accounts[0] });  

正确,工作电话

const tokenResponse = await instance.acquireTokenSilent({...accessTokenRequest, account: accounts[0]});

而 accessTokenRequest 是

export const accessTokenRequest = {
    scopes: ["api://my-app-guid/tasks.read"], 
    forceRefresh: false
};

但非常具有误导性的是,对 API 的请求实际上已经通过,并传递了授权属性。唯一不起作用的是缓存。

© www.soinside.com 2019 - 2024. All rights reserved.