Skip to content

Commit 89b1697

Browse files
committed
Add token credential as singletol
1 parent 7a77f1c commit 89b1697

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

api/Configurations/CustomServiceConfigurations.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public static TokenCredential CreateRuntimeCredential(IConfiguration config)
231231
public static IServiceCollection ConfigureDatabase(
232232
this IServiceCollection services,
233233
IConfiguration configuration,
234-
string environmentName
234+
string environmentName,
235+
TokenCredential runtimeCredential
235236
)
236237
{
237238
Console.WriteLine("Configuring Database...");
@@ -304,7 +305,11 @@ string environmentName
304305
Console.WriteLine(
305306
"Trying AppRegIdentity (Entra ID token) for PostgreSQL..."
306307
);
307-
ConfigureDatabaseWithAppRegIdentity(services, configuration);
308+
ConfigureDatabaseWithAppRegIdentity(
309+
services,
310+
configuration,
311+
runtimeCredential
312+
);
308313
Console.WriteLine("AppRegIdentity configured successfully.");
309314
configured = true;
310315
}
@@ -362,7 +367,8 @@ string environmentName
362367

363368
private static void ConfigureDatabaseWithAppRegIdentity(
364369
IServiceCollection services,
365-
IConfiguration configuration
370+
IConfiguration configuration,
371+
TokenCredential runtimeCredential
366372
)
367373
{
368374
var server = configuration["Database:Server"];
@@ -381,15 +387,13 @@ IConfiguration configuration
381387
"Database:User is required for AppRegIdentity auth."
382388
);
383389

384-
var credential = CreateRuntimeCredential(configuration);
385-
386390
Console.WriteLine("Requesting Entra ID token via credential...");
387391
var tokenRequestContext = new TokenRequestContext([AzurePostgresScope]);
388392
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
389393
AccessToken token;
390394
try
391395
{
392-
token = credential.GetToken(tokenRequestContext, cts.Token);
396+
token = runtimeCredential.GetToken(tokenRequestContext, cts.Token);
393397
}
394398
catch (OperationCanceledException oce)
395399
{
@@ -415,14 +419,13 @@ IConfiguration configuration
415419
{
416420
o.ConfigureDataSource(ds =>
417421
{
418-
var dsCredential = CreateRuntimeCredential(configuration);
419422
ds.UsePeriodicPasswordProvider(
420423
async (_, ct) =>
421424
{
422425
using var tokenCts = new CancellationTokenSource(
423426
TimeSpan.FromSeconds(5)
424427
);
425-
var accessToken = await dsCredential.GetTokenAsync(
428+
var accessToken = await runtimeCredential.GetTokenAsync(
426429
new TokenRequestContext([AzurePostgresScope]),
427430
CancellationTokenSource
428431
.CreateLinkedTokenSource(ct, tokenCts.Token)

api/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@
3737
}
3838
}
3939

40-
// Register the runtime credential (ClientSecret / WorkloadIdentity) as a singleton.
41-
builder.Services.AddSingleton<TokenCredential>(
42-
CustomServiceConfigurations.CreateRuntimeCredential(builder.Configuration)
43-
);
40+
var runtimeCredential = CustomServiceConfigurations.CreateRuntimeCredential(builder.Configuration);
41+
builder.Services.AddSingleton<TokenCredential>(runtimeCredential);
4442

4543
var applicationName = builder.Configuration["AppName"] ?? "SaraBackend";
4644

4745
builder.ConfigureLogger();
4846

49-
builder.Services.ConfigureDatabase(builder.Configuration, builder.Environment.EnvironmentName);
47+
builder.Services.ConfigureDatabase(
48+
builder.Configuration,
49+
builder.Environment.EnvironmentName,
50+
runtimeCredential
51+
);
5052
builder.Services.ConfigureMQTT();
5153

5254
var openTelemetryEnabled = builder.Configuration.GetValue<bool?>("OpenTelemetry:Enabled") ?? false;

0 commit comments

Comments
 (0)