Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="9.5.2" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="9.5.2" />
<PackageVersion Include="Azure.Core" Version="1.47.1" />
<PackageVersion Include="Azure.Identity" Version="1.15.0" />
<PackageVersion Include="Azure.Identity" Version="1.17.1" />
<PackageVersion Include="Azure.Monitor.Ingestion" Version="1.1.2" />
<PackageVersion Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
Expand All @@ -34,6 +34,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.10" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.38.1" />
<PackageVersion Include="Microsoft.Azure.StackExchangeRedis" Version="3.3.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Primitives" Version="9.0.0" />
Expand All @@ -46,7 +47,7 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<!--When updating Microsoft.Data.SqlClient, update license URL in scripts/notice-generation.ps1-->
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.3" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.1.2" />
Expand Down
1 change: 1 addition & 0 deletions src/Service/Azure.DataApiBuilder.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="Microsoft.Azure.StackExchangeRedis" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
Expand Down
31 changes: 30 additions & 1 deletion src/Service/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.IO.Abstractions;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
Expand Down Expand Up @@ -435,7 +437,7 @@ public void ConfigureServices(IServiceCollection services)
else
{
// NOTE: this is done to reuse the same connection multiplexer for both the cache and backplane
Task<ConnectionMultiplexer> connectionMultiplexerTask = ConnectionMultiplexer.ConnectAsync(level2CacheOptions.ConnectionString);
Task<IConnectionMultiplexer> connectionMultiplexerTask = CreateConnectionMultiplexerAsync(level2CacheOptions.ConnectionString);

fusionCacheBuilder
.WithSerializer(new FusionCacheSystemTextJsonSerializer())
Expand Down Expand Up @@ -470,6 +472,33 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
}

/// <summary>
/// Creates a ConnectionMultiplexer for Redis with support for Azure Entra authentication.
/// </summary>
/// <param name="connectionString">The Redis connection string.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the connected IConnectionMultiplexer.</returns>
private static async Task<IConnectionMultiplexer> CreateConnectionMultiplexerAsync(string connectionString)
{
ConfigurationOptions options = ConfigurationOptions.Parse(connectionString);

// Determine if an endpoint is localhost/loopback
static bool IsLocalhostEndpoint(EndPoint ep) => ep switch
{
DnsEndPoint dns => string.Equals(dns.Host, "localhost", StringComparison.OrdinalIgnoreCase),
IPEndPoint ip => IPAddress.IsLoopback(ip.Address),
_ => false,
};

// If no password is provided, and the endpoint (or at least one of them) is non-localhost,
// attempt to use Entra authentication.
if (string.IsNullOrEmpty(options.Password) && options.EndPoints.Any(static ep => !IsLocalhostEndpoint(ep)))
{
options = await options.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
}

return await ConnectionMultiplexer.ConnectAsync(options);
Comment on lines +496 to +499
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CreateConnectionMultiplexerAsync method lacks error handling for Azure credential failures or Redis connection failures. When DefaultAzureCredential fails to authenticate (e.g., no managed identity is available, or credentials are misconfigured), or when ConfigureForAzureWithTokenCredentialAsync fails, the method will throw an unhandled exception. Consider adding try-catch blocks with more descriptive error messages to help users troubleshoot authentication or connection issues.

Suggested change
options = await options.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
}
return await ConnectionMultiplexer.ConnectAsync(options);
try
{
options = await options.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
}
catch (CredentialUnavailableException ex)
{
throw new InvalidOperationException(
"Failed to acquire Azure credentials for Redis. " +
"Ensure that a managed identity or other Azure credentials are correctly configured " +
"for this environment before enabling Entra authentication for Redis.",
ex);
}
catch (AuthenticationFailedException ex)
{
throw new InvalidOperationException(
"Azure authentication for Redis failed while using DefaultAzureCredential. " +
"Verify that the configured identity has permission to access the Redis resource " +
"and that all required environment settings are correct.",
ex);
}
}
try
{
return await ConnectionMultiplexer.ConnectAsync(options);
}
catch (RedisConnectionException ex)
{
throw new InvalidOperationException(
"Failed to connect to Redis using the provided configuration. " +
"Verify that the Redis connection string, network connectivity, and Redis instance " +
"configuration are correct.",
ex);
}

Copilot uses AI. Check for mistakes.
}

/// <summary>
/// Configure GraphQL services within the service collection of the
/// request pipeline.
Expand Down