The loadExternalTokens() API
MSAL Browser 自 2.17.0 版起新增了 loadExternalTokens() API,可將 ID、存取權杖和重新整理權杖載入 MSAL 快取中,之後即可使用 acquireTokenSilent() 擷取這些權杖。
注意:這是一項進階功能,僅供瀏覽器環境測試用途。 將代幣載入應用程式快取可能會導致應用程式當機。 此外,我們建議 loadExternalTokens() 在單元測試與整合測試中使用 API。 關於端對端測試,請參考我們的 TestingSample 。
此 loadExternalTokens() API 是一個公開 API,方便應用程式自訂載入 MSAL 快取的憑證。
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions,
);
loadExternalTokens() 接收型別 SilentRequest為 的請求 、型別為 ExternalTokenResponse的回應,以及型別 LoadTokenOptions為 的選項。
請參閱各自的型別定義,這些定義可從 @azure/msal-browser 匯入:
載入標記
你可以提供任意組合的 id、存取權和刷新權杖來進行快取,但至少 loadExternalTokens API 需要以下一組輸入參數來識別權杖關聯並適當快取:
- 一個
SilentRequest包含 帳戶資訊的物件,或 - 具有該權限的
SilentRequest物件,以及具有clientInfo的LoadTokenOptions物件,或 - 一個具有授權的
SilentRequest物件,以及一個具有client_info的伺服器回應物件 - 一個具有
SilentRequest權限的物件,以及一個具有id_token的伺服器回應物件
以下範例顯示了每個代幣的載入,但你可以在單一請求中提供任意一個、兩個或全部三個。
載入 id 標記
除了 上述 參數外,還提供以下條件以載入 id token:
- 包含
id_token欄位的伺服器回應
根據上述資訊,快取中也會建立一個帳號。
請參考以下程式碼範例:
const config: Configuration = {
auth: { clientId: "your-client-id" },
};
const silentRequest: SilentRequest = {
account: {
homeAccountId: "your-home-account-id",
environment: "login.microsoftonline.com",
tenantId: "your-tenant-id",
username: "test@contoso.com",
localAccountId: "your-local-account-id",
},
};
const serverResponse: ExternalTokenResponse = {
id_token: "id-token-here",
};
const loadTokenOptions: LoadTokenOptions = {};
const pca = new PublicClientApplication(config);
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions
);
// OR
const config: Configuration = {
auth: { clientId: "your-client-id" },
};
const silentRequest: SilentRequest = {
scopes: [],
authority: "https://login.microsoftonline.com/your-tenant-id",
};
const serverResponse: ExternalTokenResponse = {
id_token: "id-token-here",
};
const loadTokenOptions: LoadTokenOptions = {
clientInfo: "client-info-here",
};
const pca = new PublicClientApplication(config);
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions
);
// OR
const config: Configuration = {
auth: { clientId: "your-client-id" },
};
const silentRequest: SilentRequest = {
scopes: [],
authority: "https://login.microsoftonline.com/your-tenant-id",
};
const serverResponse: ExternalTokenResponse = {
id_token: "id-token-here",
client_info: "client-info-here",
};
const loadTokenOptions: LoadTokenOptions = {};
const pca = new PublicClientApplication(config);
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions
);
載入存取權杖
除了 上述 參數外,還提供以下條件以載入存取權杖:
- 伺服器回應為
access_token、expires_in、token_type和scope
請參考以下程式碼範例:
const config: Configuration = {
auth: { clientId: "your-client-id" },
};
const silentRequest: SilentRequest = {
scopes: ["User.Read", "email"],
account: {
homeAccountId: "your-home-account-id",
environment: "login.microsoftonline.com",
tenantId: "your-tenant-id",
username: "test@contoso.com",
localAccountId: "your-local-account-id",
},
};
const serverResponse: ExternalTokenResponse = {
token_type: AuthenticationScheme.BEARER, // "Bearer"
scope: "User.Read email",
expires_in: 3599,
access_token: "access-token-here",
};
const loadTokenOptions: LoadTokenOptions = {
extendedExpiresOn: 6599,
};
const pca = new PublicClientApplication(config);
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions
);
載入刷新標記
除了 上述 參數外,還提供以下資料以載入刷新令牌:
- 包含
refresh_token及選用的refresh_token_expires_in的伺服器回應
請參考以下程式碼範例:
const config: Configuration = {
auth: { clientId: "your-client-id" },
};
const silentRequest: SilentRequest = {
scopes: [],
account: {
homeAccountId: "your-home-account-id",
environment: "login.microsoftonline.com",
tenantId: "your-tenant-id",
username: "test@contoso.com",
localAccountId: "your-local-account-id",
},
};
const serverResponse: ExternalTokenResponse = {
refresh_token: "refresh-token-here",
refresh_token_expires_in: "86399",
};
const loadTokenOptions: LoadTokenOptions = {};
const pca = new PublicClientApplication(config);
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions
);