使用语音 SDK 访问语音服务时,可以使用三种身份验证方法:服务密钥、基于密钥的令牌和Microsoft Entra ID。 本文介绍如何配置 AI Services 资源并创建语音 SDK 配置对象,以使用Microsoft Entra ID进行身份验证。
本文介绍如何将Microsoft Entra身份验证与语音 SDK 配合使用。 您将会学习如何:
- 创建 AI 服务资源
- 配置语音资源用于Microsoft Entra身份验证
- 获取Microsoft Entra访问令牌
- 创建适当的 SDK 配置对象。
若要详细了解 Microsoft Entra 访问令牌(包括令牌生存期),请访问 Microsoft 标识平台 中的访问令牌。
创建 AI 服务资源
若要在 Azure 门户中创建 AI Services 资源,请参阅本快速入门。
配置用于 Microsoft Entra 身份验证的语音资源
若要配置语音资源进行Microsoft Entra身份验证,请创建自定义域名并分配角色。
创建自定义域名
按照以下步骤为 Azure AI 服务的语音资源创建自定义子域名。
注意
启用自定义域名时,此操作不可逆。 返回区域名称的唯一方法是创建一个新的语音资源。
如果语音资源具有大量通过 Speech Studio 创建的关联自定义模型和项目,则强烈建议在修改生产中使用的资源之前,使用测试资源尝试配置。
若要使用 Azure 门户创建自定义域名,请执行以下步骤:
转到 Azure 门户并登录到Azure帐户。
选择所需语音资源。
在左窗格的“资源管理”组中,选择“网络” 。
在“防火墙和虚拟网络”选项卡上,选择“生成自定义域名” 。 一个新的右面板随即出现,其中包含有关为资源创建唯一自定义子域的说明。
在“生成自定义域名”面板中,输入自定义域名。 完整自定义域会如下所示:
https://{your custom name}.cognitiveservices.azure.cn。请记住,创建自定义域名后,无法更改域名。
输入自定义域名后,请选择“保存”。
操作完成后,在“资源管理”组中,选择“密钥和终结点” 。 确认资源的新终结点名称以这种方式开始:
https://{your custom name}.cognitiveservices.azure.cn。
分配角色
若要使用语音资源Microsoft Entra身份验证,需要分配认知服务语音参与者或认知服务语音用户角色。
可以使用 Azure 门户或 PowerShell 向用户或应用程序分配角色。
获取Microsoft Entra访问令牌
若要在 C# 中获取Microsoft Entra访问令牌,请使用 Azure Identity Client Library。
下面是使用 Azure 标识从交互式浏览器获取Microsoft Entra访问令牌的示例:
TokenRequestContext context = new Azure.Core.TokenRequestContext(new string[] { "https://cognitiveservices.azure.cn/.default" });
InteractiveBrowserCredential browserCredential = new InteractiveBrowserCredential();
var browserToken = browserCredential.GetToken(context);
string aadToken = browserToken.Token;
注意
必须将令牌上下文设置为 "https://cognitiveservices.azure.cn/.default""。
若要在 C++ 中获取Microsoft Entra访问令牌,请使用 Azure 标识客户端库。
下面是使用 Azure Identity 凭证,包括租户 ID、客户端 ID 和客户端密码,获取 Microsoft Entra 访问令牌的示例:
const std::string tokenContext = "https://cognitiveservices.azure.cn/.default";
Azure::Identity::DefaultAzureCredential();
Azure::Core::Credentials::TokenRequestContext context;
context.Scopes.push_back(tokenContext);
auto token = cred.GetToken(context, Azure::Core::Context());
注意
必须将令牌上下文设置为 "https://cognitiveservices.azure.cn/.default""。
若要在 Java 中获取Microsoft Entra访问令牌,请使用 Azure 标识客户端库。
下面是使用 Azure 标识从浏览器获取Microsoft Entra访问令牌的示例:
TokenRequestContext context = new TokenRequestContext();
context.addScopes("https://cognitiveservices.azure.cn/.default");
InteractiveBrowserCredentialBuilder builder = new InteractiveBrowserCredentialBuilder();
InteractiveBrowserCredential browserCredential = builder.build();
AccessToken browserToken = browserCredential.getToken(context).block();
String token = browserToken.getToken();
注意
必须将令牌上下文设置为 "https://cognitiveservices.azure.cn/.default""。
若要在 Python 中获取Microsoft Entra访问令牌,请使用 Azure 标识客户端库。
下面是使用 Azure 标识从交互式浏览器获取Microsoft Entra访问令牌的示例:
from azure.identity import InteractiveBrowserCredential
ibc = InteractiveBrowserCredential()
aadToken = ibc.get_token("https://cognitiveservices.azure.cn/.default")
更多示例
在 Microsoft 标识平台 代码示例中查找获取Microsoft Entra访问令牌的示例。
对于没有Microsoft 标识平台客户端库的编程语言,可以直接请求访问令牌。
获取语音资源 ID
在尚不支持 Entra ID 的情况下,您需要使用您的语音资源 ID 和 Microsoft Entra 身份验证来进行 SDK 调用。
若要在Azure门户中获取资源 ID,请执行以下操作:
- 转到 Azure 门户并登录到Azure帐户。
- 选择 AI 服务资源。
- 在左窗格的“资源管理”组中,选择“属性”。
- 复制资源 ID
创建语音 SDK 配置对象
使用Microsoft Entra访问令牌,现在可以创建语音 SDK 配置对象。
提供令牌的方法和构造相应语音 SDK Config 对象的方法因要使用的对象而异。
SpeechRecognizer、SourceLanguageRecognizer、ConversationTranscriber
对于 SpeechRecognizer、SourceLanguageRecognizer 和 ConversationTranscriber 对象,使用相应的 TokenCredential 实例进行身份验证,并使用包含您的 自定义域 的终结点来创建 SpeechConfig 对象。
TokenCredential browserCredential = new InteractiveBrowserCredential();
// Define the custom domain endpoint for your Speech resource.
var endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechConfig object using the custom domain endpoint and TokenCredential.
var speechConfig = SpeechConfig.FromEndpoint(new Uri(endpoint), browserCredential);
翻译识别器
对于 TranslationRecognizer 对象,请使用 相应的 TokenCredential 实例进行身份验证,以及包含 自定义域的终结点来创建 SpeechTranslationConfig 对象。
TokenCredential browserCredential = new InteractiveBrowserCredential();
// Define the custom domain endpoint for your Speech resource
var endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechTranslationConfig object using the custom domain endpoint and TokenCredential.
var speechConfig = SpeechTranslationConfig.FromEndpoint(new Uri(endpoint), browserCredential);
语音合成器
对于 SpeechSynthesizer 对象,请从资源 ID 和Microsoft Entra访问令牌生成授权令牌,然后使用它创建 SpeechConfig 对象。
string resourceId = "Your Resource ID";
string aadToken = "Your Microsoft Entra access token";
string region = "Your Speech Region";
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var speechConfig = SpeechConfig.FromAuthorizationToken(authorizationToken, region);
SpeechRecognizer、SpeechSynthesizer、ConversationTranscriber
对于 SpeechRecognizer、SpeechSynthesizer、ConversationTranscriber 对象,从资源 ID 和Microsoft Entra访问令牌生成授权令牌,然后使用它创建 SpeechConfig 对象。
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Microsoft Entra access token";
std::string region = "Your Speech Region";
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechConfig::FromAuthorizationToken(authorizationToken, region);
翻译识别器
对于 TranslationRecognizer,请从资源 ID 和Microsoft Entra访问令牌生成授权令牌,然后使用它创建 SpeechTranslationConfig 对象。
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Microsoft Entra access token";
std::string region = "Your Speech Region";
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechTranslationConfig::FromAuthorizationToken(authorizationToken, region);
SpeechRecognizer、ConversationTranscriber
对于 SpeechRecognizer 和 ConversationTranscriber 对象,请使用适当的 TokenCredential 实例进行身份验证,并使用包含您 自定义域 的终结点来创建 SpeechConfig 对象。
TokenCredential browserCredential = new InteractiveBrowserCredentialBuilder().build();
// Define the custom domain endpoint for your Speech resource.
String endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechConfig object using the custom domain endpoint and TokenCredential.
SpeechConfig speechConfig = SpeechConfig.fromEndpoint(new java.net.URI(endpoint), browserCredential);
翻译识别器
对于 TranslationRecognizer 对象,请使用 相应的 TokenCredential 实例进行身份验证,以及包含 自定义域的终结点来创建 SpeechTranslationConfig 对象。
TokenCredential browserCredential = new InteractiveBrowserCredentialBuilder().build();
// Define the custom domain endpoint for your Speech resource
String endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechTranslationConfig object using the custom domain endpoint and TokenCredential.
SpeechConfig speechConfig = SpeechTranslationConfig.fromEndpoint(new java.net.URI(endpoint), browserCredential);
语音合成器
对于 SpeechSynthesizer,对象从资源 ID 和Microsoft Entra访问令牌生成授权令牌,然后使用它创建 SpeechConfig 对象。
String resourceId = "Your Resource ID";
String region = "Your Region";
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
SpeechConfig speechConfig = SpeechConfig.fromAuthorizationToken(authorizationToken, region);
SpeechRecognizer、ConversationTranscriber
对于 SpeechRecognizer 和 ConversationTranscriber 对象,请使用适当的 TokenCredential 实例进行身份验证,并使用包含您 自定义域 的终结点来创建 SpeechConfig 对象。
browserCredential = InteractiveBrowserCredential()
// Define the custom domain endpoint for your Speech resource.
custom_endpoint = "https://{your custom name}.cognitiveservices.azure.cn/"
// Create the SpeechConfig object using the custom domain endpoint and TokenCredential.
speechConfig = SpeechConfig(token_credential=credential, endpoint=custom_endpoint)
翻译识别器 (TranslationRecognizer)
对于 TranslationRecognizer 对象,请使用 相应的 TokenCredential 实例进行身份验证,以及包含 自定义域的终结点来创建 SpeechTranslationConfig 对象。
browserCredential = InteractiveBrowserCredential()
// Define the custom domain endpoint for your Speech resource
custom_endpoint = "https://{your custom name}.cognitiveservices.azure.cn/"
// Create the SpeechTranslationConfig object using the custom domain endpoint and TokenCredential.
speechTranslationConfig = SpeechTranslationConfig(token_credential=credential, endpoint=custom_endpoint)
语音合成器
对于 SpeechSynthesizer 对象,请从资源 ID 和Microsoft Entra访问令牌生成授权令牌,然后使用它创建 SpeechConfig 对象。
resourceId = "Your Resource ID"
region = "Your Region"
# You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
speechConfig = SpeechConfig(auth_token=authorizationToken, region=region)
VoiceProfileClient
若要将 VoiceProfileClient 用于Microsoft Entra身份验证,请使用上面创建的自定义域名。
string customDomainName = "Your Custom Name";
string hostName = $"https://{customDomainName}.cognitiveservices.azure.cn/";
string token = "Your Microsoft Entra access token";
var config = SpeechConfig.FromHost(new Uri(hostName));
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
config.AuthorizationToken = authorizationToken;
std::string customDomainName = "Your Custom Name";
std::string aadToken = "Your Microsoft Entra access token";
auto speechConfig = SpeechConfig::FromHost("https://" + customDomainName + ".cognitiveservices.azure.cn/");
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
speechConfig->SetAuthorizationToken(authorizationToken);
String aadToken = "Your Microsoft Entra access token";
String customDomainName = "Your Custom Name";
String hostName = "https://" + customDomainName + ".cognitiveservices.azure.cn/";
SpeechConfig speechConfig = SpeechConfig.fromHost(new URI(hostName));
// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
speechConfig.setAuthorizationToken(authorizationToken);
Python 语音 SDK 不支持 VoiceProfileClient。
注意
ConversationTranslator不支持Microsoft Entra身份验证。