此頁面顯示支援的驗證方法和用戶端,並顯示可讓您使用服務連接器將 Azure Cosmos DB for Apache Cassandra 連線至其他雲端服務的範例程式碼。 在未使用服務連接器的情況下,您仍可透過其他程式設計語言連線至 Azure Cosmos DB for Cassandra。 此頁面也顯示您在建立服務連線時取得的預設環境變數名稱和值 (或 Spring Boot 設定)。
支援的計算服務
服務連接器可用來將下列計算服務連線至 Azure Cosmos DB for Apache Cassandra:
Azure App Service
Azure 容器應用程式
Azure Functions
Azure Kubernetes Service (AKS)
Azure Spring Apps
支援的驗證類型和用戶端類型
下表說明使用服務連接器將計算服務連線至 Azure Cosmos DB for Apache Cassandra 時,支援哪些用戶端類型和驗證方法的組合。 「是」表示支援的組合,而「否」則表示不支援。
用戶端類型
系統指派的受控識別
使用者指派的受控識別
祕密 / 連接字串
服務主體
.NET
Yes
.是
.是
Yes
Go
Yes
.是
.是
Yes
Java
Yes
.是
.是
Yes
Java - Spring Boot
No
無
.是
No
Node.js
Yes
.是
.是
Yes
Python
Yes
.是
.是
Yes
無
Yes
.是
.是
Yes
此表指出表中所有用戶端類型和驗證方法的組合均受支援,但 Java - Spring Boot 用戶端類型 (其僅支援秘密/連接字串方法) 除外。 所有其他用戶端類型都可以使用任何驗證方法,使用服務連接器連線至 Azure Cosmos DB for Apache Cassandra。
預設環境變數名稱或應用程式屬性和範例程式碼
根據連線的驗證類型和用戶端類型,參考下表中的連線詳細資料和範例程式碼,將計算服務連線至 Azure Cosmos DB for Apache Cassandra。 如需命名慣例的詳細資訊,請參閱服務連接器內部一文。
使用用戶端程式庫 Azure.Identity 取得受控識別或服務主體的存取權杖。 使用存取權杖和 AZURE_COSMOS_LISTKEYURL 取得密碼。 從服務連接器新增的環境變數取得連線資訊,並連線至 Azure Cosmos DB for Cassandra。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Cassandra;
using Azure.Identity;
public class Program
{
public static async Task Main()
{
var cassandraContactPoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_CONTACTPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var cassandraPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var cassandraKeyspace = Environment.GetEnvironmentVariable("AZURE_COSMOS_KEYSPACE");
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Cassandra
var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);
options.SetHostNameResolver((ipAddress) => cassandraContactPoint);
Cluster cluster = Cluster
.Builder()
.WithCredentials(userName, password)
.WithPort(cassandraPort)
.AddContactPoint(cassandraContactPoint).WithSSL(options).Build();
ISession session = await cluster.ConnectAsync();
}
public static bool ValidateServerCertificate
(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}
使用用戶端程式庫 Azure.Identity 取得受控識別或服務主體的存取權杖。 使用存取權杖和 AZURE_COSMOS_LISTKEYURL 取得密碼。 從服務連接器新增的環境變數取得連線資訊,並連線至 Azure Cosmos DB for Cassandra。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Cassandra;
using Azure.Identity;
public class Program
{
public static async Task Main()
{
var cassandraContactPoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_CONTACTPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var cassandraPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var cassandraKeyspace = Environment.GetEnvironmentVariable("AZURE_COSMOS_KEYSPACE");
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Cassandra
var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);
options.SetHostNameResolver((ipAddress) => cassandraContactPoint);
Cluster cluster = Cluster
.Builder()
.WithCredentials(userName, password)
.WithPort(cassandraPort)
.AddContactPoint(cassandraContactPoint).WithSSL(options).Build();
ISession session = await cluster.ConnectAsync();
}
public static bool ValidateServerCertificate
(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}
從服務連接器新增的環境變數取得連線資訊,並連線至 Azure Cosmos DB for Cassandra。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Cassandra;
public class Program
{
public static async Task Main()
{
var cassandraContactPoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_CONTACTPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var password = Environment.GetEnvironmentVariable("AZURE_COSMOS_PASSWORD");
var cassandraPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var cassandraKeyspace = Environment.GetEnvironmentVariable("AZURE_COSMOS_KEYSPACE");
var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);
options.SetHostNameResolver((ipAddress) => cassandraContactPoint);
Cluster cluster = Cluster
.Builder()
.WithCredentials(userName, password)
.WithPort(cassandraPort)
.AddContactPoint(cassandraContactPoint).WithSSL(options).Build();
ISession session = await cluster.ConnectAsync();
}
public static bool ValidateServerCertificate
(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}
從服務連接器新增的環境變數取得連線資訊,並連線至 Azure Cosmos DB for Cassandra。
const cassandra = require("cassandra-driver");
let username = process.env.AZURE_COSMOS_USERNAME;
let password = process.env.AZURE_COSMOS_PASSWORD;
let contactPoint = process.env.AZURE_COSMOS_CONTACTPOINT;
let port = process.env.AZURE_COSMOS_PORT;
let keyspace = process.env.AZURE_COSMOS_KEYSPACE;
let authProvider = new cassandra.auth.PlainTextAuthProvider(
username,
password
);
let client = new cassandra.Client({
contactPoints: [`${contactPoint}:${port}`],
authProvider: authProvider,
localDataCenter: 'datacenter1',
sslOptions: {
secureProtocol: "TLSv1_2_method"
},
});
client.connect();
使用用戶端程式庫 Azure.Identity 取得受控識別或服務主體的存取權杖。 使用存取權杖和 AZURE_COSMOS_LISTKEYURL 取得密碼。 從服務連接器新增的環境變數取得連線資訊,並連線至 Azure Cosmos DB for Cassandra。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Cassandra;
using Azure.Identity;
public class Program
{
public static async Task Main()
{
var cassandraContactPoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_CONTACTPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var cassandraPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var cassandraKeyspace = Environment.GetEnvironmentVariable("AZURE_COSMOS_KEYSPACE");
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Cassandra
var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);
options.SetHostNameResolver((ipAddress) => cassandraContactPoint);
Cluster cluster = Cluster
.Builder()
.WithCredentials(userName, password)
.WithPort(cassandraPort)
.AddContactPoint(cassandraContactPoint).WithSSL(options).Build();
ISession session = await cluster.ConnectAsync();
}
public static bool ValidateServerCertificate
(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}