你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用语音 SDK 的 Microsoft Entra 身份验证

使用语音 SDK 访问语音服务时,有三种身份验证方法可用:服务密钥、基于密钥的令牌和 Microsoft Entra ID。 本文介绍如何配置语音资源并创建语音 SDK 配置对象,以使用Microsoft Entra ID 进行身份验证。

本文介绍如何使用语音 SDK 的 Microsoft Entra 身份验证。 学习如何:

  • 创建语音资源
  • 为 Microsoft Entra 身份验证配置语音资源
  • 获取 Microsoft Entra 访问令牌
  • 创建适当的 SDK 配置对象。

若要详细了解 Microsoft Entra 访问令牌(包括令牌生存期),请访问 Microsoft 标识平台中的访问令牌

创建语音资源

若要在 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 标识来用租户 ID、客户端 ID 和客户端机密凭据获取 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 标识平台代码示例中获取 Microsoft Entra 访问令牌的示例。

对于 Microsoft 标识平台客户端库不可用的编程语言,可以直接请求访问令牌

获取语音资源 ID

需要语音资源 ID,以使用 Microsoft Entra 身份验证进行 SDK 调用。

注意

对于意向识别,请使用 LUIS 预测资源 ID。

若要在 Azure 门户中获取资源 ID:

  1. 转到 Azure 门户并登录 Azure 帐户。
  2. 选择语音资源。
  3. 在左窗格的“资源管理”组中,选择“属性”。
  4. 复制“资源 ID”

创建语音 SDK 配置对象

使用 Microsoft Entra 访问令牌,现在可以创建语音 SDK 配置对象。

提供令牌的方法和构造相应语音 SDK Config 对象的方法因要使用的对象而异。

SpeechRecognizer、SpeechSynthesizer、IntentRecognizer、ConversationTranscriber

对于 SpeechRecognizerSpeechSynthesizerIntentRecognizerConversationTranscriber 对象,请根据资源 ID 和 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,请根据资源 ID 和 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 对象,请根据资源 ID 和 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);

VoiceProfileClient 不可用于适用于 Python 的语音 SDK。

注意

ConversationTranslator 不支持 Microsoft Entra 身份验证。