Skip to content

Commit d21a19d

Browse files
Enhance managed identity authentication logic (#876)
* Enhance managed identity authentication logic Added a using directive for `Azure.Provisioning.Roles` and refactored the `ConfigureForManagedIdentityAuthentication` method. The method now retrieves provisionable resources more efficiently and correctly uses the `PrincipalId` from `UserAssignedIdentity` for setting the `azureClientId` metadata. * Refactor variable names for clarity in AzureRedisCacheDaprHostingExtensions
1 parent 0963e91 commit d21a19d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis/AzureRedisCacheDaprHostingExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Azure.Provisioning.AppContainers;
55
using Azure.Provisioning.Expressions;
66
using Azure.Provisioning.KeyVault;
7+
using Azure.Provisioning.Roles;
78
using CommunityToolkit.Aspire.Hosting.Azure.Dapr;
89
using CommunityToolkit.Aspire.Hosting.Dapr;
910
using AzureRedisResource = Azure.Provisioning.Redis.RedisResource;
@@ -96,7 +97,10 @@ private static void ConfigureForManagedIdentityAuthentication(this IResourceBuil
9697
{
9798
var redisHostParam = redisBuilder.GetOutput(daprConnectionStringKey).AsProvisioningParameter(infrastructure, redisHostKey);
9899

99-
if (infrastructure.GetProvisionableResources().OfType<ContainerAppManagedEnvironment>().FirstOrDefault() is ContainerAppManagedEnvironment managedEnvironment)
100+
var provisionableResources = infrastructure.GetProvisionableResources();
101+
if (provisionableResources.OfType<ContainerAppManagedEnvironment>().FirstOrDefault()
102+
is ContainerAppManagedEnvironment managedEnvironment &&
103+
provisionableResources.OfType<UserAssignedIdentity>().FirstOrDefault() is UserAssignedIdentity identity)
100104
{
101105
var daprComponent = AzureDaprHostingExtensions.CreateDaprComponent(
102106
builder.Resource.Name,
@@ -111,7 +115,7 @@ private static void ConfigureForManagedIdentityAuthentication(this IResourceBuil
111115
new() { Name = redisHostKey, Value = redisHostParam },
112116
new() { Name = "enableTLS", Value = "true" },
113117
new() { Name = "useEntraID", Value = "true" },
114-
new() { Name = "azureClientId", Value = managedEnvironment.Identity.PrincipalId }
118+
new() { Name = "azureClientId", Value = identity.PrincipalId }
115119
};
116120

117121
// Add state-specific metadata

0 commit comments

Comments
 (0)