共用方式為


適用於 JavaScript 的 Azure 通訊身分識別用戶端連結庫 - 1.3.1 版

身分識別連結庫用於管理 Azure 通訊服務的使用者和令牌。

開始使用

Prerequisites

安裝

npm install @azure/communication-identity

瀏覽器支援

JavaScript 套件組合

若要在瀏覽器中使用此用戶端連結庫,您必須先使用套件組合器。 如需如何執行這項操作的詳細資訊,請參閱我們的 統合檔

重要概念

用戶端

提供 CommunicationIdentityClient 管理使用者及其令牌的方法。

範例

驗證

您可以從 Azure 入口網站中的通訊服務資源取得金鑰和/或 連接字串。 取得金鑰之後,您可以使用下列任何方法驗證 CommunicationIdentityClient

在初始化用戶端之前先建立KeyCredentialAzureKeyCredential

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

const credential = new AzureKeyCredential(KEY);
const client = new CommunicationIdentityClient(ENDPOINT, credential);

使用 連接字串

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

const connectionString = `endpoint=ENDPOINT;accessKey=KEY`;
const client = new CommunicationIdentityClient(connectionString);

使用 TokenCredential

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

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

如果您使用金鑰來初始化用戶端,您也需要提供適當的端點。 您可以從 Azure 入口網站中的通訊服務資源取得此端點。

使用方式

建立 CommunicationIdentityClient 的實例

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

const client = new CommunicationIdentityClient(CONNECTION_STRING);

建立新使用者

createUser使用方法來建立新的使用者。

const user = await client.createUser();

建立及重新整理使用者令牌

getToken使用方法來發行或重新整理現有使用者的令牌。 方法也會採用通訊令牌範圍清單。 範圍選項包括:

  • chat (使用此選項來完整存取聊天 API)
  • voip (使用此選項來完整存取呼叫 API)
  • chat.join (聊天 API 的存取權,但沒有建立、刪除或更新聊天線程的授權,)
  • chat.join.limited (不允許新增或移除參與者的 chat.join 版本較有限,)
  • voip.join (呼叫 API 的存取權,但沒有啟動新話叫的授權)
let { token } = await client.getToken(user, ["chat"]);

若要重新整理使用者令牌,請使用相同的用戶發行另一個令牌。

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

使用自定義到期建立使用者令牌

您也可以自定義到期時間來建立通訊識別存取令牌。 令牌的有效期間必須介於 [60,1440] 分鐘範圍內。 如果未提供,則會使用預設值 1440 分鐘 (24 小時) 。

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

在單一要求中建立使用者和令牌

為了方便起見,請使用 createUserAndToken 建立新的使用者,並使用一個函式呼叫發出令牌。 這會轉譯成單一 Web 要求,而不是先建立使用者,然後再發行令牌。

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

在單一要求中建立具有自定義到期的使用者和令牌

您也可以自定義到期時間來建立通訊識別存取令牌。 令牌的有效期間必須介於 [60,1440] 分鐘範圍內。 如果未提供,則會使用預設值 1440 分鐘 (24 小時) 。

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

撤銷使用者的令牌

revokeTokens使用方法來撤銷使用者的所有已發行令牌。

await client.revokeTokens(user);

正在刪除使用者

deleteUser使用方法來刪除使用者。

await client.deleteUser(user);

交換 Teams 使用者的 Azure AD 存取令牌以進行通訊存取令牌

使用 getTokenForTeamsUser 方法來交換 Teams 使用者的 Azure AD 存取令牌,以取得具有相符到期時間的新 CommunicationAccessToken 使用者。

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>",
});

疑難排解

後續步驟

如需如何使用此連結庫的詳細 範例 ,請參閱範例目錄。

參與

如果您希望向此程式庫投稿,請參閱投稿指南,深入瞭解如何組建與測試程式碼。

曝光數