@@ -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 )
0 commit comments