利用語音 SDK 進行 Microsoft Entra 驗證

使用語音 SDK 存取語音服務時,有三種可用的驗證方法:服務金鑰、金鑰型權杖,以及 Microsoft Entra ID。 本文說明如何設定語音資源,並建立語音 SDK 設定物件以使用 Microsoft Entra ID 進行驗證。

本文說明如何搭配使用 Microsoft Entra 驗證與語音 SDK。 您將學習如何:

  • 建立語音資源
  • 設定 Microsoft Entra 驗證的語音資源
  • 取得 Microsoft Entra 存取權杖
  • 建立適當的 SDK 設定物件。

若要深入了解 Microsoft Entra 存取權杖,包括權杖存留期,請瀏覽 Microsoft 身分識別平台中的存取權杖

建立語音資源

若要在 Azure 入口網站中建立語音資源,請參閱本快速入門

設定 Microsoft Entra 驗證的語音資源

如要設定 Microsoft Entra 驗證的語音資源,請建立自訂網域名稱並指派角色。

建立自訂網域名稱

請遵循下列步驟,為您的語音資源建立 Azure AI 服務的自訂子網域名稱

警告

當您開啟自訂網域名稱時,此作業無法復原。 回復為區域名稱的唯一方法,即是建立新的語音資源。

若您的語音資源具有眾多透過 Speech Studio 建立的相關聯自訂模型及專案,強烈建議您先嘗試使用測試資源進行設定,再修改生產環境中所用的資源。

如要使用 Azure 入口網站建立自訂網域名稱,請遵循下列步驟:

  1. 前往 Azure 入口網站並登入 Azure 帳戶。

  2. 選取所需的語音資源。

  3. 在左窗格的 [資源管理] 群組中,選取 [網路]

  4. 在 [防火牆與虛擬網路] 索引標籤上,選取 [產生自訂網域名稱]。 此時會開啟一個新的右面板,說明如何為您的資源建立唯一自訂子網域。

  5. 在 [產生自訂網域名稱] 面板中,輸入自訂網域名稱。 您的完整自訂網域看起來會像這樣:https://{your custom name}.cognitiveservices.azure.com

    請記住,自訂網域名稱一經建立即無法變更。

    輸入自訂網域名稱後,選取 [儲存]

  6. 作業完成後,在 [資源管理] 群組中選取 [金鑰與端點]。 確認資源的新端點名稱是以這種方式啟動:https://{your custom name}.cognitiveservices.azure.com

指派角色

針對語音資源的 Microsoft Entra 驗證,您必須指派 [認知服務語音參與者][認知服務語音使用者] 角色。

您可以使用 Azure 入口網站PowerShell,將角色指派給使用者或應用程式。

取得 Microsoft Entra 存取權杖

如要在 C# 中取得 Microsoft Entra 存取權杖,請使用 Azure 身分識別用戶端程式庫

以下是使用 Azure 身分識別從互動式瀏覽器取得 Microsoft Entra 存取權杖的範例:

TokenRequestContext context = new Azure.Core.TokenRequestContext(new string[] { "https://cognitiveservices.azure.com/.default" });
InteractiveBrowserCredential browserCredential = new InteractiveBrowserCredential();
var browserToken = browserCredential.GetToken(context);
string aadToken = browserToken.Token;

權杖內容必須設定為 "https://cognitiveservices.azure.com/.default"。

如要在 C++ 中取得 Microsoft Entra 存取權杖,請使用 Azure 身分識別用戶端程式庫

以下範例說明如何使用 Azure 身分識別,透過您的租用戶識別碼、用戶端識別碼和用戶端密碼認證取得 Microsoft Entra 存取權杖:

const std::string tenantId = "Your Tenant ID";
const std::string clientId = "Your Client ID";
const std::string clientSecret = "Your Client Secret";
const std::string tokenContext = "https://cognitiveservices.azure.com/.default";

Azure::Identity::ClientSecretCredential cred(tenantId,
    clientId,
    clientSecret,
    Azure::Identity::ClientSecretCredentialOptions());

Azure::Core::Credentials::TokenRequestContext context;
context.Scopes.push_back(tokenContext);

auto token = cred.GetToken(context, Azure::Core::Context());

權杖內容必須設定為 "https://cognitiveservices.azure.com/.default"。

如要在 JAVA 中取得 Microsoft Entra 存取權杖,請使用 Azure 身分識別用戶端程式庫

以下是使用 Azure 身分識別從瀏覽器取得 Microsoft Entra 存取權杖的範例:

TokenRequestContext context = new TokenRequestContext();
context.addScopes("https://cognitiveservices.azure.com/.default");

InteractiveBrowserCredentialBuilder builder = new InteractiveBrowserCredentialBuilder();
InteractiveBrowserCredential browserCredential = builder.build();

AccessToken browserToken = browserCredential.getToken(context).block();
String token = browserToken.getToken();

權杖內容必須設定為 "https://cognitiveservices.azure.com/.default"。

如要在 Python 中取得 Microsoft Entra 存取權杖,請使用 Azure 身分識別用戶端程式庫

以下是使用 Azure 身分識別從互動式瀏覽器取得 Microsoft Entra 存取權杖的範例:

from azure.identity import  InteractiveBrowserCredential
ibc = InteractiveBrowserCredential()
aadToken = ibc.get_token("https://cognitiveservices.azure.com/.default")

Microsoft 身分識別平台程式碼範例中,尋找取得 Microsoft Entra 存取權杖的範例。

針對無法使用 Microsoft 身分識別平台用戶端程式庫的程式設計語言,您可以直接要求存取權杖

取得語音資源識別碼

您必須具有語音資源識別碼,才能使用 Microsoft Entra 驗證執行 SDK 呼叫。

注意

針對「意圖辨識」,請使用您 LUIS 預測資源識別碼。

如要在 Azure 入口網站中取得資源識別碼:

  1. 前往 Azure 入口網站並登入 Azure 帳戶。
  2. 選取語音資源。
  3. 在左窗格的 [資源管理] 群組中,選取 [屬性]
  4. 複製 [資源識別碼]

建立語音 SDK 設定物件

您現可使用 Microsoft Entra 存取權杖,建立語音 SDK 設定物件。

提供權杖的方法,以及用來建構對應語音 SDK Config 物件的方法,會因您所使用的物件而有所不同。

SpeechRecognizer、SpeechSynthesizer、IntentRecognizer、ConversationTranscriber

針對 SpeechRecognizerSpeechSynthesizerIntentRecognizerConversationTranscriber 物件,從資源識別碼和 Microsoft Entra 存取權杖建置授權權杖,然後用來建立 SpeechConfig 物件。

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var speechConfig = SpeechConfig.FromAuthorizationToken(authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Azure AD access token";
std::string region = "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechConfig::FromAuthorizationToken(authorizationToken, region);
String resourceId = "Your Resource ID";
String region = "Your Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
SpeechConfig speechConfig = SpeechConfig.fromAuthorizationToken(authorizationToken, region);
resourceId = "Your Resource ID"
region = "Your Region"
# You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
speechConfig = SpeechConfig(auth_token=authorizationToken, region=region)

TranslationRecognizer

針對 TranslationRecognizer,從資源識別碼和 Microsoft Entra 存取權杖建置授權權杖,然後用來建立 SpeechTranslationConfig 物件。

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var speechConfig = SpeechTranslationConfig.FromAuthorizationToken(authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Azure AD access token";
std::string region = "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechTranslationConfig::FromAuthorizationToken(authorizationToken, region);
String resourceId = "Your Resource ID";
String region = "Your Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
SpeechTranslationConfig translationConfig = SpeechTranslationConfig.fromAuthorizationToken(authorizationToken, region);
resourceId = "Your Resource ID"
region = "Your Region"

# You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
translationConfig = SpeechTranslationConfig(auth_token=authorizationToken, region=region)

DialogServiceConnector

針對 DialogServiceConnection 物件,從資源識別碼和 Microsoft Entra 存取權杖建置授權權杖,然後用來建立 CustomCommandsConfigBotFrameworkConfig 物件。

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";
string appId = "Your app ID";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var customCommandsConfig = CustomCommandsConfig.FromAuthorizationToken(appId, authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Azure AD access token";
std::string region = "Your Speech Region";
std::string appId = "Your app Id";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto customCommandsConfig = CustomCommandsConfig::FromAuthorizationToken(appId, authorizationToken, region);
String resourceId = "Your Resource ID";
String region = "Your Region";
String appId = "Your AppId";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
CustomCommandsConfig dialogServiceConfig = CustomCommandsConfig.fromAuthorizationToken(appId, authorizationToken, region);

Python 目前不支援 DialogServiceConnector

VoiceProfileClient

如要使用 VoiceProfileClient 搭配 Microsoft Entra 驗證,請使用以上建立的自訂網域名稱。

string customDomainName = "Your Custom Name";
string hostName = $"https://{customDomainName}.cognitiveservices.azure.com/";
string token = "Your Azure AD access token";

var config =  SpeechConfig.FromHost(new Uri(hostName));

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
config.AuthorizationToken = authorizationToken;
std::string customDomainName = "Your Custom Name";
std::string aadToken = "Your Azure AD access token";

auto speechConfig = SpeechConfig::FromHost("https://" + customDomainName + ".cognitiveservices.azure.com/");

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
speechConfig->SetAuthorizationToken(authorizationToken);
String aadToken = "Your Azure AD access token";
String customDomainName = "Your Custom Name";
String hostName = "https://" + customDomainName + ".cognitiveservices.azure.com/";
SpeechConfig speechConfig = SpeechConfig.fromHost(new URI(hostName));

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;

speechConfig.setAuthorizationToken(authorizationToken);

適用於 Python 的語音 SDK 無法使用 VoiceProfileClient

注意

ConversationTranslator 不支援 Microsoft Entra 驗證。