Akun di MSAL Node

Artikel ini memperlihatkan kepada Anda cara menggunakan msal-node pustaka untuk mengakses akun yang di-cache di aplikasi Node.js Anda. API, getAllAccounts(), getAccountByHomeId(), dan getAccountByLocalId() tercakup

Prasyarat

Penggunaan

Pustaka msal-node menyediakan API berbeda berikut untuk mengakses akun yang di-cache:

  • getAllAccounts(): mengembalikan semua akun yang saat ini ada di cache. Aplikasi harus memilih akun untuk memperoleh token secara diam-diam.
  • getAccountByHomeId(): menerima homeAccountId string dan mengembalikan akun yang cocok dari cache.
  • getAccountByLocalId(): menerima localAccountId string dan mengembalikan akun yang cocok dari cache.

Berikut ini adalah contoh penggunaan untuk setiap API:

getAllAccounts

Untuk skenario beberapa akun:

// Initiates Acquire Token Silent flow
function callAcquireTokenSilent() {
    // Find all accounts
    const msalTokenCache = myMSALObj.getTokenCache();
    const cachedAccounts = await msalTokenCache.getAllAccounts();

    // Account selection logic would go here

    const account = .... // Select Account code

    // Build silent request after account is selected
    const silentRequest = {
        account: account,
        scopes: scopes,
    };

    // Acquire Token Silently to be used in MS Graph call
    myMSALObj.acquireTokenSilent(silentRequest)
        .then((response) => {
            // Successful response handling
        })
        .catch((error) => {
            // Error handling
        });
}

getAccountByHomeId dan getAccountByLocalId

Untuk skenario akun tunggal, homeAccountId atau localAccountId harus diperoleh dari objek AuthResponse awal yang diterima dari alur otorisasi interaktif, seperti alur Auth Code.


// Initialize global homeAccountId variable, ideally stored in application state
let homeAccountId = null; // Same for localAccountId

// Get MSAL Token Cache from MSAL Client Applicaiton object
const msalTokenCache = myMSALObj.getTokenCache();

// Initial token acquisition, second leg of Auth Code flow
function getTokenAuthCode() {
    const tokenRequest = {
        code: req.query.code,
        redirectUri: "http://localhost:3000/redirect",
        scopes: scopes,
    };

    myMSALObj.acquireTokenByCode(tokenRequest).then((response) => {
        // Home account ID or local account ID to be used to find the right account before acquireTokenSilent
        homeAccountId = response.account.homeAccountId; // Same for localAccountId
        .
        .
        .
        // Handle successful token response
    }).catch((error) => {
        // Handle token request error
    });
}

Setelah akun dan token disimpan dalam cache dan status aplikasi berisi string homeAccountId atau localAccountId, getAccountByHomeId dan getAccountByLocalId dapat digunakan sebelum panggilan acquireTokenSilent:

async function getResource() {
    // Find account using homeAccountId or localAccountId built after receiving auth code token response
    const account = await msalTokenCache.getAccountByHomeId(app.locals.homeAccountId); // alternativley: await msalTokenCache.getAccountByLocalId(localAccountId) if using localAccountId

    // Build silent request
    const silentRequest = {
        account: account,
        scopes: scopes,
    };
    // Acquire Token Silently to be used in Resource API call
    pca.acquireTokenSilent(silentRequest)
        .then((response) => {
            // Handle successful resource API response
        })
        .catch((error) => {
            // Handle resource API request error
        });
}

Untuk skenario beberapa akun, harap ubah sampel (dalam /graphCall rute) untuk mencantumkan semua akun yang di-cache dan memilih akun tertentu. Anda mungkin juga perlu mengkustomisasi templat tampilan dan handlebars param templat terkait.

Baca juga

  • Contoh silent-flow msal-node saat ini memiliki skenario akun tunggal yang berfungsi menggunakan getAccountByHomeId().