Azure Managed Redis authentication
Use Respire.Azure to authenticate to Azure Managed Redis with Microsoft Entra ID. Install it with
Azure.Identity:
dotnet add package Respire.Azure
dotnet add package Azure.Identity
using global::Azure.Identity;
using Respire;
using Respire.Azure;
var credential = new DefaultAzureCredential();
var redisUserObjectId = "<managed-identity-or-service-principal-object-id>";
var provider = new RespireAzureManagedRedisCredentialProvider(credential, redisUserObjectId);
await using var redis = await RespireClient.ConnectAsync(new RespireOptions
{
Endpoints = { new RespireEndpoint("my-cache.eastus.redis.azure.net", 10000) },
UseTls = true,
Protocol = RespireProtocol.Resp3,
CredentialProvider = provider,
});
Setup:
- Add the managed identity or service principal as a Redis user on the resource's Authentication page.
- Use that identity's object ID as the Redis username.
- Connect to
<cache-name>.<region>.redis.azure.neton TLS port 10000.
RespireAzureManagedRedisCredentialProvider requests the https://redis.azure.com/.default scope and
passes the token's ExpiresOn to Respire's credential renewal. The
provider and TokenCredential stay caller-owned.
The provider calls TokenCredential.GetTokenAsync on every request and relies on the credential's
own caching. The production credentials behind DefaultAzureCredential cache tokens until shortly
before expiry. Add caching to a custom TokenCredential that lacks it, or large pools will each
call Microsoft Entra ID.
Use RESP3 so subscriptions can renew credentials on the same socket. Respire rejects provider-backed
subscriptions with RESP2 or Auto, because RESP2 does not allow AUTH while subscribed.
For dependency injection, assign the provider through RespireOptionsBuilder.CredentialProvider:
using global::Azure.Identity;
using Microsoft.Extensions.DependencyInjection;
using Respire;
using Respire.Azure;
using Respire.DependencyInjection;
var credential = new DefaultAzureCredential();
var redisUserObjectId = "<managed-identity-or-service-principal-object-id>";
var provider = new RespireAzureManagedRedisCredentialProvider(credential, redisUserObjectId);
var services = new ServiceCollection();
services.AddRespire(options =>
{
options.Endpoints.Add(new RespireEndpoint("my-cache.eastus.redis.azure.net", 10000));
options.UseTls = true;
options.Protocol = RespireProtocol.Resp3;
options.CredentialProvider = provider;
});
See the Respire.Samples.Azure
sample, and the Microsoft guides for Azure Managed Redis Microsoft Entra authentication
and Azure Identity credentials.