@@ -650,25 +650,44 @@ public static PSCredential GetRepositoryCredentialFromSecretManagement(
650
650
}
651
651
}
652
652
653
- public static string GetAzAccessToken ( )
653
+ public static string GetAzAccessToken ( PSCmdlet cmdletPassedIn )
654
654
{
655
+ cmdletPassedIn . WriteVerbose ( "Getting Azure access token using DefaultAzureCredential" ) ;
656
+
655
657
var credOptions = new DefaultAzureCredentialOptions
656
658
{
657
- ExcludeEnvironmentCredential = true ,
658
- ExcludeVisualStudioCodeCredential = true ,
659
- ExcludeVisualStudioCredential = true ,
660
- ExcludeWorkloadIdentityCredential = true ,
661
- ExcludeManagedIdentityCredential = true , // ManagedIdentityCredential makes the experience slow
662
- ExcludeSharedTokenCacheCredential = true , // SharedTokenCacheCredential is not supported on macOS
663
- ExcludeAzureCliCredential = false ,
664
- ExcludeAzurePowerShellCredential = false ,
665
- ExcludeInteractiveBrowserCredential = false
659
+ ExcludeEnvironmentCredential = true ,
660
+ ExcludeVisualStudioCodeCredential = true ,
661
+ ExcludeVisualStudioCredential = true ,
662
+ ExcludeWorkloadIdentityCredential = true ,
663
+ ExcludeManagedIdentityCredential = true , // ManagedIdentityCredential makes the experience slow
664
+ ExcludeSharedTokenCacheCredential = true , // SharedTokenCacheCredential is not supported on macOS
665
+ ExcludeAzureCliCredential = false ,
666
+ ExcludeAzurePowerShellCredential = false ,
667
+ ExcludeInteractiveBrowserCredential = false
666
668
} ;
667
669
668
670
var dCred = new DefaultAzureCredential ( credOptions ) ;
669
671
var tokenRequestContext = new TokenRequestContext ( new string [ ] { "https://management.azure.com/.default" } ) ;
670
- var token = dCred . GetTokenAsync ( tokenRequestContext ) . Result ;
671
- return token . Token ;
672
+
673
+ try
674
+ {
675
+ using ( var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) )
676
+ {
677
+ var token = dCred . GetTokenAsync ( tokenRequestContext , cts . Token ) . GetAwaiter ( ) . GetResult ( ) ;
678
+ return token . Token ;
679
+ }
680
+ }
681
+ catch ( OperationCanceledException )
682
+ {
683
+ cmdletPassedIn . WriteWarning ( "Timeout occurred while acquiring Azure access token." ) ;
684
+ throw ;
685
+ }
686
+ catch ( Exception ex )
687
+ {
688
+ cmdletPassedIn . WriteWarning ( $ "Failed to acquire Azure access token: { ex . Message } ") ;
689
+ throw ;
690
+ }
672
691
}
673
692
674
693
public static string GetContainerRegistryAccessTokenFromSecretManagement (
0 commit comments