Delen via


Microsoft Authentication Extensions voor Node

Met de Microsoft Authentication Extensions voor Node kunnen ontwikkelaars cross-platform token cache-serialisatie en -persistentie naar de schijf uitvoeren. Het biedt extra ondersteuning voor de Microsoft Authentication Library (MSAL) voor Node.

MsAL voor Node ondersteunt standaard een cache in het geheugen en biedt de ICachePlugin-interface voor het uitvoeren van cacheserialisatie, maar biedt geen standaardoptie voor het opslaan van de tokencache op schijf. De Microsoft Authentication Extensions for Node is de standaard implementatie voor het behouden van cache naar schijf op verschillende platforms.

De Microsoft Authentication Extensions for Node ondersteunen de volgende platforms:

  • Windows - DPAPI (Data Protection API) wordt gebruikt voor beveiliging.
  • Mac: de Mac-sleutelhanger wordt gebruikt.
  • Linux - LibSecret wordt gebruikt voor het opslaan in 'Secret Service'.

Installatie

Het msal-node-extensions pakket is beschikbaar op Node Package Manager (NPM).

npm i @azure/msal-node-extensions --save

De tokencache configureren

Hier volgt een voorbeeld van code die gebruikmaakt van Microsoft Authentication Extensions for Node om de tokencache te configureren.

const {
  DataProtectionScope,
  Environment,
  PersistenceCreator,
  PersistenceCachePlugin,
} = require("@azure/msal-node-extensions");

// You can use the helper functions provided through the Environment class to construct your cache path
// The helper functions provide consistent implementations across Windows, Mac and Linux.
const cachePath = path.join(Environment.getUserRootDirectory(), "./cache.json");

const persistenceConfiguration = {
  cachePath,
  dataProtectionScope: DataProtectionScope.CurrentUser,
  serviceName: "<SERVICE-NAME>",
  accountName: "<ACCOUNT-NAME>",
  usePlaintextFileOnLinux: false,
};

// The PersistenceCreator obfuscates a lot of the complexity by doing the following actions for you :-
// 1. Detects the environment the application is running on and initializes the right persistence instance for the environment.
// 2. Performs persistence validation for you.
// 3. Performs any fallbacks if necessary.
PersistenceCreator.createPersistence(persistenceConfiguration).then(
  async (persistence) => {
    const publicClientConfig = {
      auth: {
        clientId: "<CLIENT-ID>",
        authority: "<AUTHORITY>",
      },

      // This hooks up the cross-platform cache into MSAL
      cache: {
        cachePlugin: new PersistenceCachePlugin(persistence),
      },
    };

    const pca = new msal.PublicClientApplication(publicClientConfig);

    // Use the public client application as required...
  }
);

De volgende tabel bevat een uitleg van alle argumenten voor de persistentieconfiguratie.

Veldnaam Beschrijving Vereist voor
cachePath Het pad naar het vergrendelingsbestand dat door de bibliotheek wordt gebruikt voor het synchroniseren van lees- en schrijfbewerkingen Windows, Mac en Linux
gegevensbeschermingsbereik Hiermee specificeert u of de gegevensbeveiliging op Windows betrekking heeft op de huidige gebruiker of de lokale computer. Ramen
servicenaam Hiermee geeft u de servicenaam die moet worden gebruikt op Mac en/of Linux Mac en Linux
accountnaam Hiermee specificeert u de accountnaam die moet worden gebruikt op Mac en/of Linux. Mac en Linux
usePlaintextFileOnLinux De vlag wordt standaard ingesteld op tekst zonder opmaak in Linux als LibSecret mislukt. Standaard ingesteld op false Linux

Volgende stappen

Zie voor meer informatie over Microsoft Authentication Extensions voor Node en MSAL Node: