/// <summary> /// Notification that is triggered after token acquisition. /// </summary> /// <param name="args">Arguments related to the cache item impacted</param> public override void AfterAccessNotification(TokenCacheNotificationArgs args) { MsalCacheHelper cacheStorage = GetMsalCacheStorage(); args.AssertNotNull(nameof(args)); try { if (args.HasStateChanged) { cacheStorage.SaveUnencryptedTokenCache(args.TokenCache.SerializeMsalV3()); } } catch (Exception) { cacheStorage.Clear(); throw; } finally { CrossPlatformLock localDispose = cacheLock; cacheLock = null; localDispose?.Dispose(); } }
/// <summary> /// Notification that is triggered after token acquisition. /// </summary> /// <param name="args">Arguments related to the cache item impacted</param> public override void AfterAccessNotification(TokenCacheNotificationArgs args) { MsalCacheHelper cacheHelper = GetMsalCacheHelper(); args.AssertNotNull(nameof(args)); try { if (args.HasStateChanged) { cacheHelper.SaveUnencryptedTokenCache(args.TokenCache.SerializeMsalV3()); } } catch (Exception) { cacheHelper.Clear(); throw; } }
public void ImportExport() { var storageBuilder = new StorageCreationPropertiesBuilder( Path.GetFileName(CacheFilePath), Path.GetDirectoryName(CacheFilePath), ClientId); storageBuilder = storageBuilder.WithMacKeyChain( serviceName: "Microsoft.Developer.IdentityService.Test", accountName: "MSALCacheTest"); // unit tests run on Linux boxes without LibSecret storageBuilder.WithLinuxUnprotectedFile(); // 1. Use MSAL to create an instance of the Public Client Application var app = PublicClientApplicationBuilder.Create(ClientId) .Build(); // 3. Create the high level MsalCacheHelper based on properties and a logger _cacheHelper = MsalCacheHelper.CreateAsync( storageBuilder.Build(), new TraceSource("MSAL.CacheExtension.Test")) .GetAwaiter().GetResult(); // 4. Let the cache helper handle MSAL's cache _cacheHelper.RegisterCache(app.UserTokenCache); // Act string dataString = "Hello World"; byte[] dataBytes = Encoding.UTF8.GetBytes(dataString); var result = _cacheHelper.LoadUnencryptedTokenCache(); Assert.AreEqual(0, result.Length); _cacheHelper.SaveUnencryptedTokenCache(dataBytes); byte[] actualData = _cacheHelper.LoadUnencryptedTokenCache(); Assert.AreEqual(dataString, Encoding.UTF8.GetString(actualData)); }
public void ImportExport() { // Arrange var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>(); var cache = new MockTokenCache(); var storage = new Storage( _storageCreationPropertiesBuilder.Build(), cacheAccessor, new TraceSourceLogger(new TraceSource("ts"))); var helper = new MsalCacheHelper(cache, storage, _logger); byte[] dataToSave = Encoding.UTF8.GetBytes("Hello World 2"); cacheAccessor.Read().Returns(Encoding.UTF8.GetBytes("Hello World")); // Act byte[] actualData = helper.LoadUnencryptedTokenCache(); helper.SaveUnencryptedTokenCache(dataToSave); // Assert Assert.AreEqual("Hello World", Encoding.UTF8.GetString(actualData)); cacheAccessor.Received().Write(dataToSave); }
/// <summary> /// Saves an unencrypted, UTF-8 encoded byte array representing an MSAL token cache. /// The save operation will persist the data in a secure location, as configured /// in Microsoft.Identity.Client.Extensions.Msal.StorageCreationProperties /// </summary> public virtual void SaveUnencryptedTokenCache(byte[] tokenCache) { _helper.SaveUnencryptedTokenCache(tokenCache); }