适用于 Node 的 Microsoft 身份验证扩展使开发人员能够进行目标为磁盘的跨平台令牌缓存序列化和暂留。 它为适用于 Node 的 Microsoft 身份验证库 (MSAL) 提供额外支持。
适用于 Node 的 MSAL 默认支持内存中缓存,并提供 ICachePlugin 接口来执行缓存序列化,但不提供将令牌缓存存储到磁盘的默认方式。 适用于 Node 的 Microsoft 身份验证扩展是跨不同平台将缓存持久保存到磁盘的默认实现。
适用于 Node 的 Microsoft 身份验证扩展支持以下平台:
- Windows - 数据保护 API (DPAPI) 用于保护。
- Mac - 使用 Mac 密钥链。
- Linux - 使用 LibSecret 来存储到“机密服务”。
Node 包管理器 (NPM) 上提供 msal-node-extensions
包。
npm i @azure/msal-node-extensions --save
下面是一个代码示例,它使用适用于 Node 的 Microsoft 身份验证扩展来配置令牌缓存。
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...
}
);
下表说明了持久性配置的所有参数。
字段名称 | 说明 | 以下操作系统所需 |
---|---|---|
cachePath | 供库用于同步读取和写入的锁定文件的路径 | Windows、Mac 和 Linux |
dataProtectionScope | 指定 Windows 中当前用户或本地计算机的数据保护范围。 | Windows |
serviceName | 指定要在 Mac 和/或 Linux 上使用的服务名称 | Mac 和 Linux |
accountName | 指定要在 Mac 和/或 Linux 上使用的帐户名称 | Mac 和 Linux |
usePlaintextFileOnLinux | 当 LibSecret 失败时在 linux 上默认设置为纯文本的标志。 默认为 false |
Linux |
若要详细了解适用于 Node 和 MSAL Node 的 Microsoft 身份验证扩展插件,请参阅: