@@ -467,78 +467,53 @@ internal static PnPConnection CreateWithCredentials(Cmdlet cmdlet, Uri url, PSCr
467
467
var context = new PnPClientContext ( url . AbsoluteUri )
468
468
{
469
469
ApplicationName = Resources . ApplicationName ,
470
- DisableReturnValueCache = true
470
+ DisableReturnValueCache = true ,
471
471
} ;
472
472
PnPConnection spoConnection = null ;
473
473
if ( ! onPrem )
474
474
{
475
475
var tenantId = string . Empty ;
476
476
try
477
477
{
478
- spoConnection = new PnPConnection ( context , ConnectionType . O365 , credentials , url . ToString ( ) , tenantAdminUrl , PnPPSVersionTag , initializationType ) ;
479
-
480
- spoConnection . ConnectionMethod = ConnectionMethod . Credentials ;
481
- spoConnection . AzureEnvironment = azureEnvironment ;
482
- spoConnection . Tenant = tenantId ;
483
- spoConnection . ClientId = clientId ;
484
-
485
- if ( ! string . IsNullOrWhiteSpace ( clientId ) )
478
+ PnP . Framework . AuthenticationManager authManager = null ;
479
+ if ( CachedAuthenticationManager != null )
486
480
{
487
- PnP . Framework . AuthenticationManager authManager = null ;
488
- if ( PnPConnection . CachedAuthenticationManager != null )
489
- {
490
- authManager = PnPConnection . CachedAuthenticationManager ;
491
- PnPConnection . CachedAuthenticationManager = null ;
492
- }
493
- else
494
- {
495
- authManager = PnP . Framework . AuthenticationManager . CreateWithCredentials ( clientId , credentials . UserName , credentials . Password , redirectUrl , azureEnvironment , tokenCacheCallback : async ( tokenCache ) =>
496
- {
497
- await MSALCacheHelper ( tokenCache , url . ToString ( ) , clientId ) ;
498
- } ) ;
499
- }
500
- using ( authManager )
501
- {
502
- context = PnPClientContext . ConvertFrom ( authManager . GetContext ( url . ToString ( ) ) ) ;
503
- context . ExecutingWebRequest += ( sender , e ) =>
504
- {
505
- e . WebRequestExecutor . WebRequest . UserAgent = $ "NONISV|SharePointPnP|PnPPS/{ ( ( AssemblyFileVersionAttribute ) Assembly . GetExecutingAssembly ( ) . GetCustomAttribute ( typeof ( AssemblyFileVersionAttribute ) ) ) . Version } ({ System . Environment . OSVersion . VersionString } )";
506
- } ;
507
- context . ExecuteQueryRetry ( ) ;
508
- Log . Debug ( "PnPConnection" , "Acquiring token" ) ;
509
- var accesstoken = authManager . GetAccessTokenAsync ( url . ToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
510
- Log . Debug ( "PnPConnection" , "Token acquired" ) ;
511
- var parsedToken = new Microsoft . IdentityModel . JsonWebTokens . JsonWebToken ( accesstoken ) ;
512
- tenantId = parsedToken . Claims . FirstOrDefault ( c => c . Type == "tid" ) . Value ;
513
- spoConnection . AuthenticationManager = authManager ;
514
- }
481
+ authManager = CachedAuthenticationManager ;
482
+ CachedAuthenticationManager = null ;
515
483
}
516
484
else
517
485
{
518
- PnP . Framework . AuthenticationManager authManager = null ;
519
- if ( PnPConnection . CachedAuthenticationManager != null )
486
+ authManager = PnP . Framework . AuthenticationManager . CreateWithCredentials ( clientId , credentials . UserName , credentials . Password , redirectUrl , azureEnvironment , tokenCacheCallback : async ( tokenCache ) =>
520
487
{
521
- authManager = PnPConnection . CachedAuthenticationManager ;
522
- }
523
- else
524
- {
525
- authManager = PnP . Framework . AuthenticationManager . CreateWithCredentials ( clientId , credentials . UserName , credentials . Password , azureEnvironment : azureEnvironment ) ;
526
- }
527
- using ( authManager )
488
+ await MSALCacheHelper ( tokenCache , url . ToString ( ) , clientId ) ;
489
+ } ) ;
490
+ }
491
+ using ( authManager )
492
+ {
493
+ var clientContext = authManager . GetContext ( url . ToString ( ) ) ;
494
+ context = PnPClientContext . ConvertFrom ( clientContext ) ;
495
+
496
+ context . ExecutingWebRequest += ( sender , e ) =>
528
497
{
529
- context = PnPClientContext . ConvertFrom ( authManager . GetContext ( url . ToString ( ) ) ) ;
530
- context . ExecutingWebRequest += ( sender , e ) =>
531
- {
532
- e . WebRequestExecutor . WebRequest . UserAgent = $ "NONISV|SharePointPnP|PnPPS/{ ( ( AssemblyFileVersionAttribute ) Assembly . GetExecutingAssembly ( ) . GetCustomAttribute ( typeof ( AssemblyFileVersionAttribute ) ) ) . Version } ({ System . Environment . OSVersion . VersionString } )";
533
- } ;
534
- context . ExecuteQueryRetry ( ) ;
535
-
536
- var accessToken = authManager . GetAccessTokenAsync ( url . ToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
537
- var parsedToken = new Microsoft . IdentityModel . JsonWebTokens . JsonWebToken ( accessToken ) ;
538
- tenantId = parsedToken . Claims . FirstOrDefault ( c => c . Type == "tid" ) . Value ;
539
- spoConnection . AuthenticationManager = authManager ;
540
- }
498
+ e . WebRequestExecutor . WebRequest . UserAgent = $ "NONISV|SharePointPnP|PnPPS/{ ( ( AssemblyFileVersionAttribute ) Assembly . GetExecutingAssembly ( ) . GetCustomAttribute ( typeof ( AssemblyFileVersionAttribute ) ) ) . Version } ({ System . Environment . OSVersion . VersionString } )";
499
+ } ;
500
+ context . ExecuteQueryRetry ( ) ;
501
+ Log . Debug ( "PnPConnection" , "Acquiring token" ) ;
502
+ var accesstoken = authManager . GetAccessTokenAsync ( url . ToString ( ) ) . GetAwaiter ( ) . GetResult ( ) ;
503
+ Log . Debug ( "PnPConnection" , "Token acquired" ) ;
504
+ var parsedToken = new Microsoft . IdentityModel . JsonWebTokens . JsonWebToken ( accesstoken ) ;
505
+ tenantId = parsedToken . Claims . FirstOrDefault ( c => c . Type == "tid" ) . Value ;
506
+
507
+ spoConnection = new PnPConnection ( context , ConnectionType . O365 , credentials , url . ToString ( ) , tenantAdminUrl , PnPPSVersionTag , initializationType ) ;
508
+
509
+ spoConnection . ConnectionMethod = ConnectionMethod . Credentials ;
510
+ spoConnection . AzureEnvironment = azureEnvironment ;
511
+ spoConnection . Tenant = tenantId ;
512
+ spoConnection . ClientId = clientId ;
513
+
514
+ spoConnection . AuthenticationManager = authManager ;
541
515
}
516
+
542
517
}
543
518
catch ( ClientRequestException )
544
519
{
@@ -1032,7 +1007,7 @@ private static async Task MSALCacheHelper(ITokenCache tokenCache, string url, st
1032
1007
}
1033
1008
catch ( MsalCachePersistenceException )
1034
1009
{
1035
- PnP . Framework . Diagnostics . Log . Debug ( "PnPConnection" , "Cache persistence failed. Trying again." ) ;
1010
+ PnP . Framework . Diagnostics . Log . Debug ( "PnPConnection" , "Cache persistence failed. Trying again." ) ;
1036
1011
var storage =
1037
1012
new StorageCreationPropertiesBuilder ( "pnp.msal.cache" , cacheDir )
1038
1013
. WithMacKeyChain (
0 commit comments