此頁面顯示支援的驗證方法和用戶端,並顯示範例程式碼,讓您可用來使用服務連接器,將 Azure Cosmos DB for NoSQL 連線到其他雲端服務。 在未使用服務連接器的情況下,您仍可透過其他程式設計語言連線至 Azure Cosmos DB for NoSQL。 此頁面也顯示您在建立服務連線時取得的預設環境變數名稱和值 (或 Spring Boot 設定)。
支援的計算服務
服務連接器可用來將下列計算服務連線至 Azure Cosmos DB for NoSQL:
Azure App Service
Azure 容器應用程式
Azure Functions
Azure Kubernetes Service (AKS)
Azure Spring Apps
支援的驗證類型和用戶端類型
下表說明使用服務連接器將計算服務連線至 Azure Cosmos DB for NoSQL 時,支援哪些用戶端類型和驗證方法的組合。 「是」表示支援的組合,而「否」則表示不支援。
用戶端類型
系統指派的受控識別
使用者指派的受控識別
祕密 / 連接字串
服務主體
.NET
Yes
.是
.是
Yes
Java
Yes
.是
.是
Yes
Java - Spring Boot
Yes
.是
.是
Yes
Node.js
Yes
.是
.是
Yes
Python
Yes
.是
.是
Yes
Go
Yes
.是
.是
Yes
無
Yes
.是
.是
Yes
下表指出支源表格中所有用戶端類型和驗證方法組合。 所有用戶端類型都可以使用任何驗證方法,使用服務連接器連線到 Azure Cosmos DB for NoSQL。
預設環境變數名稱或應用程式屬性和範例程式碼
使用下列連線的詳細資料,將您的計算服務連線至 Azure Cosmos DB for NoSQL。 針對下列範例,將預留位置文字 <database-server>、<database-name>、<account-key>、<resource-group-name>、<subscription-ID>、<client-ID>、<SQL-server>、<client-secret>、<tenant-id> 和 <access-key> 取代為您自己的資訊。 如需命名慣例的詳細資訊,請參閱服務連接器內部一文。
系統指派的受控識別
SpringBoot 用戶端類型
使用系統指派的受控識別作為驗證類型僅適用於 Spring Cloud Azure 4.0 版或更高版本。
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// 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 credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// 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 credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});
using Microsoft.Azure.Cosmos;
using System;
// Create a new instance of CosmosClient using a connection string
using CosmosClient client = new(
connectionString: Environment.GetEnvironmentVariable("AZURE_COSMOS_CONNECTIONSTRING")!
);
import os
from azure.cosmos import CosmosClient
# Create a new instance of CosmosClient using a connection string
CONN_STR = os.environ["AZURE_COSMOS_CONNECTIONSTRING"]
client = CosmosClient.from_connection_string(conn_str=CONN_STR)
安裝相依性。
go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
import { CosmosClient } from "@azure/cosmos";
// Create a new instance of CosmosClient using a connection string
const cosmosClient = new CosmosClient(process.env.AZURE_COSMOS_CONNECTIONSTRING);
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// 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 credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});