Share via


使用語音 SDK 進行 Microsoft Entra 驗證

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

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

  • 建立語音資源
  • 設定 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"

若要在 Java 中取得 Microsoft Entra 存取令牌,請使用 Azure 身分識別客戶端連結庫

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

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

在程式代碼範例尋找取得 Microsoft Entra 存取令牌的範例 Microsoft 身分識別平台。

對於無法使用 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)

DialogService 連線 or

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 目前不支援 DialogService 連線 or

VoiceProfileClient

若要搭配 Microsoft Entra 驗證使用 VoiceProfileClient ,請使用上面建立的自定義功能變數名稱。

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);

VoiceProfileClient不適用於適用於 Python 的語音 SDK。

注意

ConversationTranslator不支援 Microsoft Entra 驗證。