Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
The loadExternalTokens() API
Browser MSAL mulai versi 2.17.0 telah menambahkan loadExternalTokens() API, yang memungkinkan pemuatan id, akses, dan refresh token ke cache MSAL, yang kemudian dapat diambil menggunakan acquireTokenSilent().
Catatan: Ini adalah fitur lanjutan yang ditujukan untuk tujuan pengujian di lingkungan browser saja. Memuat token ke cache aplikasi Anda dapat menyebabkan aplikasi Anda rusak. Selain itu, kami menyarankan loadExternalTokens() API untuk digunakan dengan pengujian unit dan integrasi. Untuk pengujian E2E, silakan lihat TestingSample kami sebagai gantinya.
loadExternalTokens() API adalah API publik yang memfasilitasi aplikasi untuk memuat token kustom ke cache MSAL.
await loadExternalTokens(
config,
silentRequest,
serverResponse,
loadTokenOptions,
);
loadExternalTokens() mengambil permintaan jenis SilentRequest, respons jenis ExternalTokenResponse, dan opsi jenis LoadTokenOptions.
Lihat definisi jenis untuk masing-masing, yang dapat diimpor dari @azure/msal-browser:
Memuat token
Anda dapat memberikan kombinasi apa pun dari token ID, akses, dan refresh untuk penyimpanan cache, namun setidaknya loadExternalTokens API memerlukan salah satu kumpulan parameter masukan berikut untuk mengidentifikasi keterkaitan token dan menyimpannya dalam cache dengan tepat:
- Objek
SilentRequestdengan informasi akun, ATAU - Sebuah objek
SilentRequestdengan otoritas DAN sebuah objekLoadTokenOptionsdenganclientInfo, ATAU - Objek
SilentRequestdengan otoritas DAN objek respons server denganclient_info - Objek
SilentRequestdengan otoritas DAN objek respons server denganid_token
Contoh di bawah ini menunjukkan pemuatan token satu per satu, namun, Anda dapat memberikan 1, 2, atau semua 3 dalam satu permintaan.
Memuat token id
Selain parameter yang tercantum di atas , berikan hal berikut untuk memuat token id:
- Respons server dengan kolom
id_token
Akun juga akan diatur dalam cache berdasarkan informasi yang diberikan di atas.
Lihat contoh kode di bawah ini:
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
);
Memuat token akses
Selain parameter yang tercantum di atas , berikan hal berikut untuk memuat token akses:
- Respons server dengan
access_token,expires_in,token_type, danscope
Lihat contoh kode di bawah ini:
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
);
Memuat token penyegaran
Selain parameter yang tercantum di atas , berikan hal berikut untuk memuat token refresh:
- Respons server dengan
refresh_tokendan opsionalrefresh_token_expires_in
Lihat contoh kode di bawah ini:
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
);