Edit

Share via


Integrate Azure Cache for Redis with Service Connector

This article covers supported authentication methods, clients, and sample code you can use to connect your apps to Azure Cache for Redis using Service Connector.In this article, you'll also find default environment variable names, values, and configuration obtained when creating service connections.

Supported compute services

You can use Service Connector to connect the following compute services to Azure Cache for Redis:

  • Azure App Service
  • Azure Container Apps
  • Azure Functions
  • Azure Kubernetes Service (AKS)
  • Azure Spring Apps

Supported authentication and client types

The following table shows which combinations of authentication methods and clients are supported for connecting your compute service to Azure Cache for Redis by using Service Connector. "Yes" means that the combination is supported. "No" means that it isn't supported.

Client type System-assigned managed identity User-assigned managed identity Secret / connection string Service principal
.NET Yes Yes Yes Yes
Go No No Yes No
Java Yes Yes Yes Yes
Java - Spring Boot No No Yes No
Node.js Yes Yes Yes Yes
Python Yes Yes Yes Yes
None Yes Yes Yes Yes

Default environment variable names or application properties and sample code

Use the following environment variable names and application properties to connect compute services to your Redis server. To learn more about naming conventions, check the Service Connector internals article.

System-assigned managed identity

Default environment variable name Description Sample value
AZURE_REDIS_HOST Redis endpoint <RedisName>.redis.cache.windows.net

Sample code

The following steps and code show you how to use a system-assigned managed identity to connect to Redis.

  1. Install dependencies.

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. Add the authentication logic with environment variables set by Service Connector. For more information, see Microsoft.Azure.StackExchangeRedis Extension.

    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);
    

User-assigned managed identity

Default environment variable name Description Sample value
AZURE_REDIS_HOST Redis endpoint <RedisName>.redis.cache.windows.net
AZURE_REDIS_CLIENTID Managed-identity client ID <client-ID>

Sample code

The following steps and code show you how to use a user-assigned managed identity to connect to Redis.

  1. Install dependencies.

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. Add the authentication logic with environment variables set by Service Connector. For more information, see Microsoft.Azure.StackExchangeRedis Extension.

    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);
    

Connection string

Warning

We recommend that you use the most secure authentication flow available. The authentication flow described here requires a very high degree of trust in the application, and carries risks that aren't present in other flows. You should use this flow only when more secure flows, such as managed identities, aren't viable.

Default environment variable name Description Example value
AZURE_REDIS_CONNECTIONSTRING StackExchange.Redis connection string <redis-server-name>.redis.cache.windows.net:6380,password=<redis-key>,ssl=True,defaultDatabase=0

Sample code

The following steps and code show you how to use a connection string to connect to Azure Cache for Redis.

  1. Install dependencies.

    dotnet add package StackExchange.Redis --version 2.6.122
    
  2. Get the connection string from the environment variable added by Service Connector.

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

Service principal

Default environment variable name Description Sample value
AZURE_REDIS_HOST Redis endpoint <RedisName>.redis.cache.windows.net
AZURE_REDIS_CLIENTID Client ID of the service principal <client-ID>
AZURE_REDIS_CLIENTSECRET Secret of the service principal <client-secret>
AZURE_REDIS_TENANTID Tenant ID of the service principal <tenant-id>

Sample code

The following steps and code show you how to use a service principal to connect to Redis.

  1. Install dependencies.

    dotnet add package Microsoft.Azure.StackExchangeRedis --version 3.2.0
    
  2. Add the authentication logic with environment variables set by Service Connector. For more information, see Microsoft.Azure.StackExchangeRedis Extension.

    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);