msal-browser 库提供以下 API 来访问缓存帐户:
getAllAccounts():返回当前在缓存中的所有帐户。应用程序必须选择一个帐户以静默获取令牌。
getAccountByHomeId():接收一个homeAccountId字符串并从缓存中返回匹配的帐户。
getAccountByLocalId():接收一个localAccountId字符串并从缓存中返回匹配的帐户。
getAccountByUsername():接收用户名字符串并从缓存中返回匹配的帐户。
[...剪断...]
当前的msal-browser默认示例有一个有效的单帐户方案。
来源:MSAL 浏览器中的帐户。
该示例代码的一部分:
const myMSALObj = new msal.PublicClientApplication(msalConfig);
myMSALObj.handleRedirectPromise().then(handleResponse).catch(err => {
console.error(err);
});
function handleResponse(resp) {
if (resp !== null) {
accountId = resp.account.homeAccountId;
myMSALObj.setActiveAccount(resp.account);
showWelcomeMessage(resp.account);
} else {
const currentAccounts = myMSALObj.getAllAccounts();
if (!currentAccounts || currentAccounts.length < 1) {
return;
} else if (currentAccounts.length === 1) {
const activeAccount = currentAccounts[0];
myMSALObj.setActiveAccount(activeAccount);
accountId = activeAccount.homeAccountId;
showWelcomeMessage(activeAccount);
}
}
}