Skip to content

Commit 51067de

Browse files
Replace DefaultAzureCredential with ManagedIdentityCredential
1 parent dc25290 commit 51067de

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

App/backend-api/Microsoft.GS.DPS.Host/AppConfiguration/AppConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Azure.Identity;
22
using Microsoft.Extensions.Azure;
33
using Microsoft.GS.DPSHost.AppConfiguration;
4+
using Microsoft.GS.DPSHost.Helpers;
45

56
namespace Microsoft.GS.DPSHost.AppConfiguration
67
{
@@ -16,7 +17,7 @@ public static void Config(IHostApplicationBuilder builder)
1617
//Read AppConfiguration with managed Identity
1718
builder.Configuration.AddAzureAppConfiguration(options =>
1819
{
19-
options.Connect(new Uri(builder.Configuration["ConnectionStrings:AppConfig"]), new DefaultAzureCredential());
20+
options.Connect(new Uri(builder.Configuration["ConnectionStrings:AppConfig"]), AzureCredentialHelper.GetAzureCredential());
2021
});
2122

2223
//Read ServiceConfiguration
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Azure.Core;
4+
using Azure.Identity;
5+
6+
namespace Microsoft.GS.DPSHost.Helpers
7+
{
8+
public static class AzureCredentialHelper
9+
{
10+
public static TokenCredential GetAzureCredential(string clientId = null)
11+
{
12+
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
13+
14+
if (string.Equals(env, "Development", StringComparison.OrdinalIgnoreCase))
15+
{
16+
return new DefaultAzureCredential(); // For local development
17+
}
18+
else
19+
{
20+
return clientId != null
21+
? new ManagedIdentityCredential(clientId)
22+
: new ManagedIdentityCredential();
23+
}
24+
}
25+
26+
public static Task<TokenCredential> GetAzureCredentialAsync(string clientId = null)
27+
{
28+
return Task.FromResult(GetAzureCredential(clientId));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)