@azure/communication-identity

中文文档
2.0.0发布于 2026/10/02MITTypeScriptESM + CJS近 30 天下载 16.9万

SDK用于Azure通信服务,方便用户代号管理.

中文使用文档
文档目录25 节
  1. 开始
  2. 先决条件
  3. 安装
  4. 浏览器支持
  5. 关键概念
  6. 客户
  7. 实例
  8. 认证
  9. 创建 KeyCredential 与 AzureKeyCredential 在初始化客户端之前
  10. 使用连接字符串
  11. 使用a TokenCredential
  12. 使用量
  13. 创建新用户
  14. 创建和刷新用户令牌
  15. 创建自定义过期的用户令牌
  16. 在单个请求中创建用户和令牌
  17. 在单个请求中创建用户和自定义过期的令牌
  18. 为用户撤销令牌
  19. 删除用户
  20. 交换 Teams 用户的 Azure AD 访问令牌用于通信访问令牌
  21. 解决问题
  22. 日志
  23. 今后的步骤
  24. 捐款
  25. 相关项目

JavaScript 的 Azure 通讯身份客户端库

身份库用于管理Azure通信服务的用户和信使.

开始

先决条件

安装

npm install @azure/communication-identity

浏览器支持

Java 脚本套装

要在浏览器中使用此客户端库,首先需要使用捆绑器. 关于如何做到这一点的详情,请参见我们 捆绑文件。 。 。 .

关键概念

客户

这个 CommunicationIdentityClient 提供管理用户及其代币的方法.

实例

认证

您可以从您的通信服务资源中获取密钥和/或连接字符串 Azure 门户。一旦你有钥匙,就可以认证 CommunicationIdentityClient 采用下列任何一种方法:

创建 KeyCredential 与 AzureKeyCredential 在初始化客户端之前

import { AzureKeyCredential } from "@azure/core-auth";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const key = "<some-key>";
const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new AzureKeyCredential(key);
const client = new CommunicationIdentityClient(endpoint, credential);

使用连接字符串

import { CommunicationIdentityClient } from "@azure/communication-identity";

// Example connection string
const connectionString =
  "endpoint=https://contoso.eastus.communications.azure.net/;accesskey=secret";

const client = new CommunicationIdentityClient(connectionString);

使用a TokenCredential

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

如果您使用密钥初始化客户端, 您还需要提供合适的端点 。 您可以从您的通信服务资源中获取此端点 Azure 门户。 。 。 .

使用量

创建新用户

使用 createUser 创建新用户的方法.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

创建和刷新用户令牌

使用 getToken 为已有用户发布或刷新一个符号的方法。 该方法还收录在通信符号范围列表中. 范围选项包括:

  • chat (用这个来全程访问Chat API)
  • voip (这个可以全程访问API)
  • chat.join (访问 Chat API 但无权创建、删除或更新聊天线索)
  • chat.join.limited (一个比较有限的聊天版本.join,不允许添加或删除参与者)
  • voip.join (进入API但无权开始新电话)
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

const { token } = await client.getToken(user, ["chat"]);

要刷新用户令牌,请与同一用户再发一个令牌.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

let { token } = await client.getToken(user, ["chat"]);

// Refresh the token again
({ token } = await client.getToken(user, ["chat"]));

创建自定义过期的用户令牌

也可以通过自定义过期时间来创建通信身份访问符. 令牌的有效期必须在[60,1440]分以内. 如果不提供,将使用1440分(24小时)的默认值.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const user = await client.createUser();

const tokenOptions = { tokenExpiresInMinutes: 60 };
const { token } = await client.getToken(user, ["chat"], tokenOptions);

在单个请求中创建用户和令牌

为方便起见 createUserAndToken 创建新用户,并发布一个函数调用符。 这可以翻译成一个单一的网络请求,而不是先创建用户,再发行一个令牌.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const { user, token } = await client.createUserAndToken(["chat"]);

在单个请求中创建用户和自定义过期的令牌

也可以通过自定义过期时间来创建通信身份访问符. 令牌的有效期必须在[60,1440]分以内. 如果不提供,将使用1440分(24小时)的默认值.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const userAndTokenOptions = { tokenExpiresInMinutes: 60 };
const { user, token } = await client.createUserAndToken(["chat"], userAndTokenOptions);

为用户撤销令牌

使用 revokeTokens 为用户撤销所有已签发的令牌的方法.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

// Create user
const user = await client.createUser();

// Later when you want to revoke the user's tokens
await client.revokeTokens(user);

删除用户

使用 deleteUser 删除用户的方法.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

// Create user
const user = await client.createUser();

// Later when you want to delete the user
await client.deleteUser(user);

交换 Teams 用户的 Azure AD 访问令牌用于通信访问令牌

使用 getTokenForTeamsUser 将 Teams 用户的 Azure AD 访问符换为新符的方法 CommunicationAccessToken 带有匹配的过期时间.

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const endpoint = "https://contoso.eastus.communications.azure.net";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);

const { token, expiresOn } = await client.getTokenForTeamsUser({
  teamsUserAadToken: "<aad-access-token-of-a-teams-user>",
  clientId: "<cliend-id-of-an-aad-application>",
  userObjectId: "<aad-object-id-of-a-teams-user>",
});

解决问题

日志

允许伐木可能有助于发现有关失败的有用信息。 为了查看 HTTP 请求和回复的日志,设置 AZURE_LOG_LEVEL 环境变量改为 info。或者,可以在运行时拨打 setLogLevel 输入 @azure/logger数字 :

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

setLogLevel("info");

今后的步骤

请看一下 样本 关于如何使用此库的详细示例的目录.

捐款

如果您想为图书馆捐钱 请读一下 贡献指南 学习如何构建和测试密码.

版本与发布记录

@azure/communication-identity 共发布 442 个版本,首次发布于 2021/01/27;过去 12 个月发布了 57 个版本。

版本发布日期距今
2.0.0latest2026/10/021 个月前
1.4.0-alpha.20260211.1dev2026/02/118 个月前
1.4.0-alpha.20260210.12026/02/108 个月前
1.4.0-alpha.20260209.12026/02/098 个月前
1.4.0-alpha.20260206.12026/02/069 个月前
1.4.0-alpha.20260205.12026/02/059 个月前

其他发布通道:next → 1.4.0-beta.1,dev → 1.4.0-alpha.20260211.1。

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

@azure/communication-identity 是什么?

SDK用于Azure通信服务,方便用户代号管理.

如何安装 @azure/communication-identity?

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

@azure/communication-identity 支持 TypeScript 吗?

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

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

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

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

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

@azure/communication-identity 可以商用吗?

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

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

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

@azure/communication-identity 还在维护吗?

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