@azure/identity-cache-persistence

中文文档
1.3.3发布于 2026/10/30MITTypeScriptESM + CJS近 30 天下载 5.3万

A secure, persistent token cache for Azure Identity credentials that uses the OS secret-management API

原始使用文档
文档目录11 节
  1. Getting started
  2. Prerequisites
  3. Install the package
  4. Key concepts
  5. Azure Identity Plugins
  6. Examples
  7. Troubleshooting
  8. Logging
  9. Next steps
  10. Provide Feedback
  11. Contributing

Azure Identity Plugin for Token Cache Persistence

This package provides a plugin to the Azure Identity library for JavaScript (@azure/identity) that enables persistent token caching. Token cache persistence allows the built-in token cache to persist across sessions using a secure storage system provided by the local operating system.

Source code | Samples

Getting started

Prerequisites

Install the package

This package is designed to be used with Azure Identity for JavaScript. Install both @azure/identity and this package using npm:

$ npm install --save @azure/identity
$ npm install --save @azure/identity-cache-persistence

Supported Environments

Azure Identity plugins for JavaScript support stable (even numbered) versions of Node.js starting from v12. While the plugins may run in other Node versions, no support is guaranteed. @azure/identity-cache-persistence does not support browser environments.

Key concepts

If this is your first time using @azure/identity or Microsoft Entra ID, we recommend that you read Using @azure/identity with Microsoft Entra ID first. This document will give you a deeper understanding of the platform and how to configure your Azure account correctly.

Azure Identity Plugins

As of @azure/identity version 2.0.0, the Identity client library for JavaScript includes a plugin API. This package (@azure/identity-cache-persistence) exports a plugin object that you must pass as an argument to the top-level useIdentityPlugin function from the @azure/identity package. Enable token cache persistence in your program as follows:

import { useIdentityPlugin } from "@azure/identity";
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";

useIdentityPlugin(cachePersistencePlugin);

After calling useIdentityPlugin, the persistent token cache plugin is registered to the @azure/identity package and will be available on all credentials that support persistent token caching (those that have tokenCachePersistenceOptions in their constructor options).

Examples

Once the plugin is registered, you can enable token cache persistence by passing tokenCachePersistenceOptions with an enabled property set to true to a credential constructor. In the following example, we use the DeviceCodeCredential, since persistent caching of its tokens allows you to skip the interactive device-code authentication flow if a cached token is available.

import { DeviceCodeCredential } from "@azure/identity";

const credential = new DeviceCodeCredential({
  tokenCachePersistenceOptions: {
    enabled: true,
  },
});

// We'll use the Microsoft Graph scope as an example
const scope = "https://graph.microsoft.com/.default";

// Print out part of the access token
console.log((await credential.getToken(scope)).token.substring(0, 10), "...");

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

Next steps

Provide Feedback

If you encounter bugs or have suggestions, please open an issue.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

版本与发布记录

@azure/identity-cache-persistence 共发布 480 个版本,首次发布于 2021/06/23;过去 12 个月发布了 69 个版本。

版本发布日期距今
1.3.3latest2026/10/308 天前
1.3.22026/09/161 个月前
1.3.12026/08/192 个月前
1.3.02026/04/106 个月前
1.2.2-alpha.20260310.1dev2026/03/117 个月前
1.2.2-alpha.20260309.22026/03/107 个月前

关于 @azure/identity-cache-persistence 的常见问题

@azure/identity-cache-persistence 是什么?

A secure, persistent token cache for Azure Identity credentials that uses the OS secret-management API

如何安装 @azure/identity-cache-persistence?

在项目目录运行 npm install @azure/identity-cache-persistence,或使用 pnpm add / yarn add / bun add。

@azure/identity-cache-persistence 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/identity-cache-persistence 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/identity-cache-persistence 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/identity-cache-persistence 可以商用吗?

@azure/identity-cache-persistence 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/identity-cache-persistence 的下载量有多少?

根据 npm 官方下载统计,@azure/identity-cache-persistence 在 2026/10/08 – 2026/11/06 这 30 天内被下载 52,728 次,日均约 1,758 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/identity-cache-persistence 还在维护吗?

最新版本 1.3.3 发布于 2026/10/30(8 天前);至今共发布 480 个版本,其中过去 12 个月发布了 69 个;首次发布于 2021/06/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。