Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
使用语音 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 服务的语音资源创建自定义子域名。
Caution
启用自定义域名时,此操作不可逆。 返回区域名称的唯一方法是创建一个新的语音资源。
如果语音资源具有大量通过 Speech Studio 创建的关联自定义模型和项目,则强烈建议在修改生产中使用的资源之前,使用测试资源尝试配置。
- Azure 门户
- PowerShell
- Azure CLI
若要使用 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;
Note
必须将令牌上下文设置为 "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());
Note
必须将令牌上下文设置为 "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();
Note
必须将令牌上下文设置为 "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 门户
- PowerShell
若要在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 对象,请使用 相应的 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);
SpeechRecognizer、SpeechSynthesizer、ConversationTranscriber
对于SpeechRecognizer和SpeechSynthesizerConversationTranscriber对象,请使用相应的 TokenCredential 实例进行身份验证。 结合包含您的自定义域的端点,使用这些凭据创建一个SpeechConfig对象。
auto browserCredential = std::make_shared<Azure::Identity::InteractiveBrowserCredential>();
// Define the custom domain endpoint for your Speech resource.
auto endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechConfig object using the custom domain endpoint and TokenCredential.
auto speechConfig = SpeechConfig::FromEndpoint(endpoint, browserCredential);
翻译识别器
TranslationRecognizer为此,请使用相应的 TokenCredential 实例进行身份验证。 结合包含您的自定义域的端点,使用这些凭据创建一个SpeechTranslationConfig对象。
auto browserCredential = std::make_shared<Azure::Identity::InteractiveBrowserCredential>();
// Define the custom domain endpoint for your Speech resource
auto endpoint = "https://{your custom name}.cognitiveservices.azure.cn/";
// Create the SpeechTranslationConfig object using the custom domain endpoint and TokenCredential.
auto speechConfig = SpeechTranslationConfig::FromEndpoint(endpoint, browserCredential);
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 对象,请使用 相应的 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);
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=browserCredential, endpoint=custom_endpoint)
翻译识别器
对于 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=browserCredential, endpoint=custom_endpoint)
语音合成器
SpeechSynthesizer对于对象,请使用相应的 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=browserCredential, endpoint=custom_endpoint)
Note
ConversationTranslator不支持Microsoft Entra身份验证。