@@ -548,149 +548,7 @@ A product-quality, file-based token cache serializer for public client applicati
548
548
549
549
#### Dual token cache serialization (MSAL unified cache and ADAL v3)
550
550
551
- If you want to implement token cache serialization with the unified cache format (common to ADAL .NET 4 .x , MSAL .NET 2 .x , and other MSALs of the same generation or older , on the same platform ), take a look at the following code :
552
-
553
- ```csharp
554
- string appLocation = Path .GetDirectoryName (Assembly .GetEntryAssembly ().Location ;
555
- string cacheFolder = Path .GetFullPath (appLocation ) + @" ..\..\..\.." );
556
- string adalV3cacheFileName = Path .Combine (cacheFolder , " cacheAdalV3.bin" );
557
- string unifiedCacheFileName = Path .Combine (cacheFolder , " unifiedCache.bin" );
558
-
559
- IPublicClientApplication app ;
560
- app = PublicClientApplicationBuilder .Create (clientId )
561
- .Build ();
562
- FilesBasedTokenCacheHelper .EnableSerialization (app .UserTokenCache ,
563
- unifiedCacheFileName ,
564
- adalV3cacheFileName );
565
-
566
- ```
567
-
568
- This time , the helper class is defined as :
569
-
570
- ```csharp
571
- using System ;
572
- using System .IO ;
573
- using System .Security .Cryptography ;
574
- using Microsoft .Identity .Client ;
575
-
576
- namespace CommonCacheMsalV3
577
- {
578
- /// <summary >
579
- /// Simple persistent cache implementation of the dual cache serialization (ADAL v3 legacy
580
- /// and unified cache format) for a desktop applications (from MSAL 2.x)
581
- /// </summary >
582
- static class FilesBasedTokenCacheHelper
583
- {
584
- /// <summary >
585
- /// Enables the serialization of the token cache
586
- /// </summary >
587
- /// <param name =" adalV3CacheFileName" >File name where the cache is serialized with the
588
- /// ADAL v3 token cache format. Can
589
- /// be <c >null</c > if you don't want to implement the legacy ADAL v3 token cache
590
- /// serialization in your MSAL 2.x+ application</param >
591
- /// <param name =" unifiedCacheFileName" >File name where the cache is serialized
592
- /// with the unified cache format, common to
593
- /// ADAL v4 and MSAL v2 and later, and also across ADAL/MSAL on the same platform.
594
- /// Should not be <c >null</c ></param >
595
- /// <returns ></returns >
596
- public static void EnableSerialization (ITokenCache tokenCache , string unifiedCacheFileName , string adalV3CacheFileName )
597
- {
598
- UnifiedCacheFileName = unifiedCacheFileName ;
599
- AdalV3CacheFileName = adalV3CacheFileName ;
600
-
601
- tokenCache .SetBeforeAccess (BeforeAccessNotification );
602
- tokenCache .SetAfterAccess (AfterAccessNotification );
603
- }
604
-
605
- /// <summary >
606
- /// File path where the token cache is serialized with the unified cache format
607
- /// (ADAL.NET v4, MSAL.NET v3)
608
- /// </summary >
609
- public static string UnifiedCacheFileName { get ; private set ; }
610
-
611
- /// <summary >
612
- /// File path where the token cache is serialized with the legacy ADAL v3 format
613
- /// </summary >
614
- public static string AdalV3CacheFileName { get ; private set ; }
615
-
616
- private static readonly object FileLock = new object ();
617
-
618
- public static void BeforeAccessNotification (TokenCacheNotificationArgs args )
619
- {
620
- lock (FileLock )
621
- {
622
- args .TokenCache .DeserializeAdalV3 (ReadFromFileIfExists (AdalV3CacheFileName ));
623
- try
624
- {
625
- args .TokenCache .DeserializeMsalV3 (ReadFromFileIfExists (UnifiedCacheFileName ));
626
- }
627
- catch (Exception ex )
628
- {
629
- // Compatibility with the MSAL v2 cache if you used one
630
- args .TokenCache .DeserializeMsalV2 (ReadFromFileIfExists (UnifiedCacheFileName ));
631
- }
632
- }
633
- }
634
-
635
- public static void AfterAccessNotification (TokenCacheNotificationArgs args )
636
- {
637
- // if the access operation resulted in a cache update
638
- if (args .HasStateChanged )
639
- {
640
- lock (FileLock )
641
- {
642
- WriteToFileIfNotNull (UnifiedCacheFileName , args .TokenCache .SerializeMsalV3 ());
643
- if (! string .IsNullOrWhiteSpace (AdalV3CacheFileName ))
644
- {
645
- WriteToFileIfNotNull (AdalV3CacheFileName , args .TokenCache .SerializeAdalV3 ());
646
- }
647
- }
648
- }
649
- }
650
-
651
- /// <summary >
652
- /// Read the content of a file if it exists
653
- /// </summary >
654
- /// <param name =" path" >File path</param >
655
- /// <returns >Content of the file (in bytes)</returns >
656
- private static byte [] ReadFromFileIfExists (string path )
657
- {
658
- byte [] protectedBytes = (! string .IsNullOrEmpty (path ) && File .Exists (path ))
659
- ? File .ReadAllBytes (path ) : null ;
660
- byte [] unprotectedBytes = encrypt ?
661
- ((protectedBytes != null ) ? ProtectedData .Unprotect (protectedBytes , null , DataProtectionScope .CurrentUser ) : null )
662
- : protectedBytes ;
663
- return unprotectedBytes ;
664
- }
665
-
666
- /// <summary >
667
- /// Writes a blob of bytes to a file. If the blob is <c >null</c >, deletes the file
668
- /// </summary >
669
- /// <param name =" path" >path to the file to write</param >
670
- /// <param name =" blob" >Blob of bytes to write</param >
671
- private static void WriteToFileIfNotNull (string path , byte [] blob )
672
- {
673
- if (blob != null )
674
- {
675
- byte [] protectedBytes = encrypt
676
- ? ProtectedData .Protect (blob , null , DataProtectionScope .CurrentUser )
677
- : blob ;
678
- File .WriteAllBytes (path , protectedBytes );
679
- }
680
- else
681
- {
682
- File .Delete (path );
683
- }
684
- }
685
-
686
- // Change if you want to test with an unencrypted blob (this is a JSON format)
687
- private static bool encrypt = true ;
688
- }
689
- }
690
- ```
691
-
692
- For more details see the sample : https :// github.com/Azure-Samples/active-directory-dotnet-v1-to-v2/tree/master/TokenCacheMigration/ADAL2MSAL
693
-
551
+ If you want to implement token cache serialization with the unified cache format (common to ADAL .NET 4 .x , MSAL .NET 2 .x , and other MSALs of the same generation or older , on the same platform ), take a look at the following sample : https :// github.com/Azure-Samples/active-directory-dotnet-v1-to-v2/tree/master/TokenCacheMigration/ADAL2MSAL.
694
552
695
553
-- -
696
554
0 commit comments