共用方式為


整合 Azure Cache for Redis 與服務連接器

本文涵蓋支援的驗證方法、用戶端和範例程式碼,您可以使用服務連接器將應用程式連線至 Azure Cache for Redis,在本文中,您也會發現建立服務連線時取得的預設環境變數名稱、值和組態。

支援的計算服務

您可以使用 Service Connector 將下列計算服務連線至 Azure Cache for Redis:

  • Azure App Service
  • Azure 容器應用程式
  • Azure Functions
  • Azure Kubernetes Service (AKS)
  • Azure Spring Apps

支援的驗證和客戶端類型

下表顯示使用服務連接器將計算服務連線到 Azure Cache for Redis 時,支援驗證方法和客戶端的組合。 “Yes” 表示支持組合。 「否」表示不支援。

用戶端類型 系統指派的受控識別 使用者指派的受控識別 祕密 / 連接字串 服務主體
.NET Yes Yes Yes Yes
Go Yes
Java Yes Yes Yes Yes
Java - Spring Boot Yes
Node.js Yes Yes Yes Yes
Python Yes Yes Yes Yes
Yes Yes Yes Yes

預設環境變數名稱或應用程式屬性和範例程式碼

使用下列環境變數名稱和應用程式屬性,將計算服務連線到您的 Redis 伺服器。 若要深入瞭解命名慣例,請參閱 Service Connector 內部文章

系統指派的受控識別

預設環境變數名稱 描述 範例值
AZURE_REDIS_HOST Redis 端點 <RedisName>.redis.cache.windows.net

範例指令碼

下列步驟和程式代碼會示範如何使用系統指派的受控識別來聯機到 Redis。

  1. 安裝依賴項目。

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. 使用 Service Connector 所設定的環境變數來新增驗證邏輯。 如需詳細資訊,請參閱 Microsoft.Azure.StackExchangeRedis 擴充功能

    using StackExchange.Redis;
    var cacheHostName = Environment.GetEnvironmentVariable("AZURE_REDIS_HOST");
    var configurationOptions = ConfigurationOptions.Parse($"{cacheHostName}:6380");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // await configurationOptions.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
    
    // For user-assigned identity.
    // var managedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // await configurationOptions.ConfigureForAzureWithUserAssignedManagedIdentityAsync(managedIdentityClientId);
    
    // Service principal secret.
    // var clientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_REDIS_TENANTID");
    // var secret = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTSECRET");
    // await configurationOptions.ConfigureForAzureWithServicePrincipalAsync(clientId, tenantId, secret);
    
    
    var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
    

使用者指派的管理識別

預設環境變數名稱 描述 範例值
AZURE_REDIS_HOST Redis 端點 <RedisName>.redis.cache.windows.net
AZURE_REDIS_CLIENTID 受控識別用戶端識別碼 <client-ID>

範例指令碼

下列步驟和程式代碼會示範如何使用使用者指派的受控識別來聯機到 Redis。

  1. 安裝相依性。

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. 使用 Service Connector 所設定的環境變數來新增驗證邏輯。 如需詳細資訊,請參閱 Microsoft.Azure.StackExchangeRedis 擴充功能

    using StackExchange.Redis;
    var cacheHostName = Environment.GetEnvironmentVariable("AZURE_REDIS_HOST");
    var configurationOptions = ConfigurationOptions.Parse($"{cacheHostName}:6380");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // await configurationOptions.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
    
    // For user-assigned identity.
    // var managedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // await configurationOptions.ConfigureForAzureWithUserAssignedManagedIdentityAsync(managedIdentityClientId);
    
    // Service principal secret.
    // var clientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_REDIS_TENANTID");
    // var secret = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTSECRET");
    // await configurationOptions.ConfigureForAzureWithServicePrincipalAsync(clientId, tenantId, secret);
    
    
    var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
    

連接字串

警告

我們建議您使用最安全的驗證流程。 此處所述的驗證流程需要應用程式中非常高度的信任,而且具有在其他流程中不存在的風險。 只有當更安全的流程,例如受控識別無法運作時,才應該使用此流程。

預設環境變數名稱 描述 範例值
AZURE_REDIS_CONNECTIONSTRING StackExchange.Redis連接字串 <redis-server-name>.redis.cache.windows.net:6380,password=<redis-key>,ssl=True,defaultDatabase=0

範例指令碼

下列步驟和程式代碼會示範如何使用 連接字串 來聯機到 Azure Cache for Redis。

  1. 安裝依賴項。

    dotnet add package StackExchange.Redis --version 2.6.122
    
  2. 從服務連接器新增的環境變數取得連接字串。

    using StackExchange.Redis;
    var connectionString = Environment.GetEnvironmentVariable("AZURE_REDIS_CONNECTIONSTRING");
    var _redisConnection = await RedisConnection.InitializeAsync(connectionString: connectionString);
    

服務主體

預設環境變數名稱 描述 範例值
AZURE_REDIS_HOST Redis 端點 <RedisName>.redis.cache.windows.net
AZURE_REDIS_CLIENTID 服務主體的用戶端識別碼 <client-ID>
AZURE_REDIS_CLIENTSECRET 服務主體的秘密 <client-secret>
AZURE_REDIS_TENANTID 服務主體的租用戶識別碼 <tenant-id>

範例指令碼

下列步驟和程式代碼會示範如何使用服務主體來連線到 Redis。

  1. 安裝依賴項。

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. 使用 Service Connector 所設定的環境變數來新增驗證邏輯。 如需詳細資訊,請參閱 Microsoft.Azure.StackExchangeRedis 擴充功能

    using StackExchange.Redis;
    var cacheHostName = Environment.GetEnvironmentVariable("AZURE_REDIS_HOST");
    var configurationOptions = ConfigurationOptions.Parse($"{cacheHostName}:6380");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // For system-assigned identity.
    // await configurationOptions.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
    
    // For user-assigned identity.
    // var managedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // await configurationOptions.ConfigureForAzureWithUserAssignedManagedIdentityAsync(managedIdentityClientId);
    
    // Service principal secret.
    // var clientId = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTID");
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_REDIS_TENANTID");
    // var secret = Environment.GetEnvironmentVariable("AZURE_REDIS_CLIENTSECRET");
    // await configurationOptions.ConfigureForAzureWithServicePrincipalAsync(clientId, tenantId, secret);
    
    
    var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);