本页提供有关支持的身份验证方法和客户端的信息。 它提供示例代码,可用于使用服务连接器将计算服务连接到 Azure AI 多服务资源。 此页还列出了创建服务连接时获取的默认环境变量名称和值。
受支持的计算服务
服务连接器可用于将以下计算服务连接到 Azure AI 多服务资源:
- Azure App 服务
- Azure 容器应用
- Azure Functions
- Azure Kubernetes 服务 (AKS)
- Azure Spring Apps
受支持的身份验证类型和客户端类型
下表指示使用服务连接器将计算服务连接到 Azure AI 多服务资源的身份验证方法和客户端。 “是”表示支持组合,而“否”则表示不支持该组合。
| 客户端类型 |
系统分配的托管标识 |
用户分配的托管标识 |
机密/连接字符串 |
服务主体 |
| .NET |
是 |
是 |
是 |
是 |
| Java |
是 |
是 |
是 |
是 |
| Node.js |
是 |
是 |
是 |
是 |
| Python |
是 |
是 |
是 |
是 |
| 无 |
是 |
是 |
是 |
是 |
此表支持表中客户端类型和身份验证方法的所有组合都受到支持。 所有客户端类型都可通过服务连接器使用任何身份验证方法连接到 Azure AI 多服务资源。
默认环境变量名称或应用程序属性和示例代码
使用以下连接详细信息将计算服务连接到 Azure AI 多服务资源。 有关详细信息,请参阅 配置命名约定。
系统分配的托管标识(推荐)
| 默认环境变量名称 |
说明 |
示例值 |
| AZURE_COGNITIVESERVICES_ENDPOINT |
Azure 认知服务令牌提供程序服务 |
https://<cognitive-service-name>.cognitiveservices.azure.com/ |
代码示例
若要使用系统分配的托管标识连接到 Azure AI 多服务资源,请参阅以下步骤和代码。
可以使用 Azure 客户端库访问 Azure AI 多服务资源支持的各种认知 API。 此示例使用 Azure AI 文本分析作为示例。 若要直接调用认知 API,请参阅 使用 Microsoft Entra ID 进行身份验证。
安装以下依赖项。 此示例使用 Azure.AI.TextAnalytics。
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
将以下依赖项添加到 pom.xml 文件。 此示例使用azure-ai-textanalytics。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COGNITIVESERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COGNITIVESERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
安装以下依赖项。 此示例使用 azure-ai-textanalytics。
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COGNITIVESERVICES_TENANTID')
# client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_COGNITIVESERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
安装以下依赖项。 此示例使用ai-text-analytics。
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
使用 @azure/identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_COGNITIVESERVICES_TENANTID;
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_COGNITIVESERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
用户分配的托管标识
| 默认环境变量名称 |
说明 |
示例值 |
| AZURE_COGNITIVESERVICES_ENDPOINT |
Azure 认知服务令牌提供程序服务 |
https://<cognitive-service-name>.cognitiveservices.azure.com/ |
| AZURE_COGNITIVESERVICES_CLIENTID |
客户端 ID |
<client-ID> |
代码示例
若要使用用户分配的托管标识连接到 Azure AI 多服务资源,请参阅以下步骤和代码。
可以使用 Azure 客户端库访问 Azure AI 多服务资源支持的各种认知 API。 此示例使用 Azure AI 文本分析作为示例。 若要直接调用认知 API,请参阅 使用 Microsoft Entra ID 进行身份验证。
安装以下依赖项。 此示例使用 Azure.AI.TextAnalytics。
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
将以下依赖项添加到 pom.xml 文件。 此示例使用azure-ai-textanalytics。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COGNITIVESERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COGNITIVESERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
安装以下依赖项。 此示例使用 azure-ai-textanalytics。
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COGNITIVESERVICES_TENANTID')
# client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_COGNITIVESERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
安装以下依赖项。 此示例使用ai-text-analytics。
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
使用 @azure/identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_COGNITIVESERVICES_TENANTID;
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_COGNITIVESERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
连接字符串
| 默认环境变量名称 |
说明 |
示例值 |
| AZURE_COGNITIVESERVICES_ENDPOINT |
Azure 认知服务令牌提供程序服务 |
https://<cognitive-service-name>.cognitiveservices.azure.com/ |
| AZURE_COGNITIVESERVICES_KEY |
Azure AI 多服务资源的 API 密钥 |
<api-key> |
代码示例
若要使用连接字符串连接到 Azure AI 多服务资源,请参阅以下步骤和代码。
可以使用 Azure 客户端库访问 Azure AI 多服务资源支持的各种认知 API。 此示例使用 Azure AI 文本分析作为示例。 若要直接调用认知 API,请参阅 使用 AI Foundry 资源密钥进行身份验证。
安装以下依赖项。 此示例使用 Azure.AI.TextAnalytics。
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Core --version 1.40.0
从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点和密钥。
using Azure.AI.TextAnalytics;
string endpoint = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_ENDPOINT")
string key = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_KEY");
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
new AzureKeyCredential(key));
将以下依赖项添加到 pom.xml 文件。 此示例使用 azure-ai-textanalytics。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.49.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点和密钥。
String endpoint = System.getenv("AZURE_COGNITIVESERVICES_ENDPOINT");
String key = System.getenv("AZURE_COGNITIVESERVICES_KEY");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
安装以下依赖项。 此示例使用 azure-ai-textanalytics。
pip install azure-ai-textanalytics==5.1.0
pip install azure-core
从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点和密钥。
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
key = os.environ['AZURE_COGNITIVESERVICES_KEY']
endpoint = os.environ['AZURE_COGNITIVESERVICES_ENDPOINT']
language_service_client = TextAnalyticsClient(
endpoint=retrieved_endpoint,
credential=AzureKeyCredential(key))
安装以下依赖项。 此示例使用 ai-text-analytics。
npm install @azure/ai-text-analytics@5.1.0
从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点和密钥。
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
const endpoint = process.env.AZURE_COGNITIVESERVICES_ENDPOINT;
const credential = new AzureKeyCredential(process.env.AZURE_COGNITIVESERVICES_KEY);
const languageClient = new TextAnalyticsClient(endpoint, credential);
服务主体
| 默认环境变量名称 |
说明 |
示例值 |
| AZURE_COGNITIVESERVICES_ENDPOINT |
Azure 认知服务令牌提供程序服务 |
https://<cognitive-service-name>.cognitiveservices.azure.com/ |
| AZURE_COGNITIVESERVICES_CLIENTID |
客户端 ID |
<client-ID> |
| AZURE_COGNITIVESERVICES_CLIENTSECRET |
客户端密码 |
<client-secret> |
| AZURE_COGNITIVESERVICES_TENANTID |
租户 ID |
<tenant-ID> |
代码示例
若要使用服务主体连接到 Azure AI 多服务资源,请参阅以下步骤和代码。
可以使用 Azure 客户端库访问 Azure AI 多服务资源支持的各种认知 API。 此示例使用 Azure AI 文本分析作为示例。 若要直接调用认知 API,请参阅 使用 Microsoft Entra ID 进行身份验证。
安装以下依赖项。 此示例使用 Azure.AI.TextAnalytics。
dotnet add package Azure.AI.TextAnalytics
dotnet add package Azure.Identity
使用 Azure 标识库进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
using Azure.AI.TextAnalytics;
using Azure.Identity;
string endpoint = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_ENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
TextAnalyticsClient languageServiceClient = new(
new Uri(endpoint),
credential);
将以下依赖项添加到 pom.xml 文件。 此示例使用azure-ai-textanalytics。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.11.4</version>
</dependency>
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COGNITIVESERVICES_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COGNITIVESERVICES_TENANTID"))
// .build();
String endpoint = System.getenv("AZURE_COGNITIVESERVICES_ENDPOINT");
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();
安装以下依赖项。 此示例使用 azure-ai-textanalytics。
pip install azure-ai-textanalytics==5.1.0
pip install azure-identity
使用 azure-identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COGNITIVESERVICES_TENANTID')
# client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID')
# client_secret = os.getenv('AZURE_COGNITIVESERVICES_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv('AZURE_COGNITIVESERVICES_ENDPOINT')
language_service_client = TextAnalyticsClient(
endpoint=endpoint,
credential=cred)
安装以下依赖项。 此示例使用ai-text-analytics。
npm install @azure/ai-text-analytics@5.1.0
npm install @azure/identity
使用 @azure/identity 进行身份验证,并从服务连接器添加的环境变量中获取 Azure AI 多服务资源终结点。 使用以下代码时,请取消注释要使用的身份验证类型的代码片段的一部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { TextAnalyticsClient } = require("@azure/ai-text-analytics");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_COGNITIVESERVICES_TENANTID;
// const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID;
// const clientSecret = process.env.AZURE_COGNITIVESERVICES_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const endpoint = process.env.AZURE_COGNITIVESERVICES_ENDPOINT;
const languageClient = new TextAnalyticsClient(endpoint, credential);
相关内容