public static void AfterAccessNotification(TokenCacheNotificationArgs args) { // if the access operation resulted in a cache update if (args.HasStateChanged) { lock (s_fileLock) { var adalV3State = args.TokenCache.SerializeAdalV3(); #pragma warning disable CS0618 // Type or member is obsolete var msalV2State = args.TokenCache.SerializeMsalV2(); #pragma warning restore CS0618 // Type or member is obsolete var msalV3State = args.TokenCache.SerializeMsalV3(); // reflect changes in the persistent store if ((s_cacheStorageType & CacheStorageType.Adal) == CacheStorageType.Adal) { if (!string.IsNullOrWhiteSpace(AdalV3CacheFileName)) { CacheFileUtils.WriteToFileIfNotNull(AdalV3CacheFileName, adalV3State); } } if ((s_cacheStorageType & CacheStorageType.MsalV2) == CacheStorageType.MsalV2) { CacheFileUtils.WriteToFileIfNotNull(MsalV2CacheFileName, msalV2State); } if ((s_cacheStorageType & CacheStorageType.MsalV3) == CacheStorageType.MsalV3) { CacheFileUtils.WriteToFileIfNotNull(MsalV3CacheFileName, msalV3State); } } } }
public static void AfterAccessNotification(TokenCacheNotificationArgs args) { // if the access operation resulted in a cache update if (args.HasStateChanged) { lock (s_fileLock) { var cacheData = args.TokenCache.SerializeUnifiedAndAdalCache(); // reflect changes in the persistent store if ((s_cacheStorage & CacheStorageType.Adal) == CacheStorageType.Adal) { if (!string.IsNullOrWhiteSpace(AdalV3CacheFileName)) { CacheFileUtils.WriteToFileIfNotNull(AdalV3CacheFileName, cacheData.AdalV3State); } } if ((s_cacheStorage & CacheStorageType.MsalV2) == CacheStorageType.MsalV2) { CacheFileUtils.WriteToFileIfNotNull(UnifiedCacheFileName, cacheData.UnifiedState); } } } }
// Triggered right after ADAL accessed the cache. private void AfterAccessNotification(TokenCacheNotificationArgs args) { // if the access operation resulted in a cache update if (HasStateChanged) { lock (s_fileLock) { // reflect changes in the persistent store var cacheData = SerializeAdalAndUnifiedCache(); if ((_cacheStorageType & CacheStorageType.Adal) == CacheStorageType.Adal) { CacheFileUtils.WriteToFileIfNotNull(AdalV3CacheFilePath, cacheData.AdalV3State); } if ((_cacheStorageType & CacheStorageType.MsalV2) == CacheStorageType.MsalV2) { CacheFileUtils.WriteToFileIfNotNull(UnifiedCacheFilePath, cacheData.UnifiedState); } // once the write operation took place, restore the HasStateChanged bit to false HasStateChanged = false; } } }
// Triggered right before ADAL needs to access the cache. // Reload the cache from the persistent store in case it changed since the last access. private void BeforeAccessNotification(TokenCacheNotificationArgs args) { lock (s_fileLock) { Deserialize(CacheFileUtils.ReadFromFileIfExists(CacheFilePath)); } }
// Triggered right after ADAL accessed the cache. private void AfterAccessNotification(TokenCacheNotificationArgs args) { // if the access operation resulted in a cache update if (HasStateChanged) { lock (s_fileLock) { var adalV3Bytes = SerializeAdalV3(); var msalV2Bytes = SerializeMsalV2(); var msalV3Bytes = SerializeMsalV3(); // reflect changes in the persistent store if ((_cacheStorageType & CacheStorageType.Adal) == CacheStorageType.Adal) { CacheFileUtils.WriteToFileIfNotNull(AdalV3CacheFilePath, adalV3Bytes); } if ((_cacheStorageType & CacheStorageType.MsalV2) == CacheStorageType.MsalV2) { CacheFileUtils.WriteToFileIfNotNull(MsalV2CacheFilePath, msalV2Bytes); } if ((_cacheStorageType & CacheStorageType.MsalV3) == CacheStorageType.MsalV3) { CacheFileUtils.WriteToFileIfNotNull(MsalV3CacheFilePath, msalV3Bytes); } // once the write operation took place, restore the HasStateChanged bit to false HasStateChanged = false; } } }
// Initializes the cache against a local file. // If the file is already present, it loads its content in the ADAL cache public FileBasedAdalV3TokenCache(string filePath) { CacheFilePath = filePath; AfterAccess = AfterAccessNotification; BeforeAccess = BeforeAccessNotification; lock (s_fileLock) { Deserialize(CacheFileUtils.ReadFromFileIfExists(CacheFilePath)); } }
public static void BeforeAccessNotification(TokenCacheNotificationArgs args) { lock (s_fileLock) { var adalv3State = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFileName); var unifiedState = CacheFileUtils.ReadFromFileIfExists(UnifiedCacheFileName); args.TokenCache.DeserializeUnifiedAndAdalCache(new CacheData { AdalV3State = adalv3State, UnifiedState = unifiedState }); } }
// Triggered right before ADAL needs to access the cache. // Reload the cache from the persistent store in case it changed since the last access. private void BeforeAccessNotification(TokenCacheNotificationArgs args) { lock (s_fileLock) { var cacheData = new CacheData { AdalV3State = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFilePath), UnifiedState = CacheFileUtils.ReadFromFileIfExists(UnifiedCacheFilePath) }; DeserializeAdalAndUnifiedCache(cacheData); } }
private void LoadCache() { lock (s_fileLock) { var adalV3Bytes = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFilePath); var msalV2Bytes = CacheFileUtils.ReadFromFileIfExists(MsalV2CacheFilePath); var msalV3Bytes = CacheFileUtils.ReadFromFileIfExists(MsalV3CacheFilePath); DeserializeAdalV3(adalV3Bytes); DeserializeMsalV2(msalV2Bytes); DeserializeMsalV3(msalV3Bytes); } }
// Triggered right after ADAL accessed the cache. private void AfterAccessNotification(TokenCacheNotificationArgs args) { // if the access operation resulted in a cache update if (HasStateChanged) { lock (s_fileLock) { // reflect changes in the persistent store CacheFileUtils.WriteToFileIfNotNull(CacheFilePath, Serialize()); // once the write operation took place, restore the HasStateChanged bit to false HasStateChanged = false; } } }
// Initializes the cache against a local file. // If the file is already present, it loads its content in the ADAL cache public FileBasedTokenCache(CacheStorageType cacheStorageType, string adalV3FilePath, string unifiedCacheFilePath) { _cacheStorageType = cacheStorageType; AdalV3CacheFilePath = adalV3FilePath; UnifiedCacheFilePath = unifiedCacheFilePath; AfterAccess = AfterAccessNotification; BeforeAccess = BeforeAccessNotification; lock (s_fileLock) { var cacheData = new CacheData { AdalV3State = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFilePath), UnifiedState = CacheFileUtils.ReadFromFileIfExists(UnifiedCacheFilePath) }; DeserializeAdalAndUnifiedCache(cacheData); } }
public static void BeforeAccessNotification(TokenCacheNotificationArgs args) { lock (s_fileLock) { var adalv3State = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFileName); var msalv2State = CacheFileUtils.ReadFromFileIfExists(MsalV2CacheFileName); var msalv3State = CacheFileUtils.ReadFromFileIfExists(MsalV3CacheFileName); if (adalv3State != null) { args.TokenCache.DeserializeAdalV3(adalv3State); } if (msalv2State != null) { args.TokenCache.DeserializeMsalV2(msalv2State); } if (msalv3State != null) { args.TokenCache.DeserializeMsalV3(msalv3State); } } }
public static void BeforeAccessNotification(TokenCacheNotificationArgs args) { lock (s_fileLock) { var adalv3State = CacheFileUtils.ReadFromFileIfExists(AdalV3CacheFileName); var msalv2State = CacheFileUtils.ReadFromFileIfExists(MsalV2CacheFileName); var msalv3State = CacheFileUtils.ReadFromFileIfExists(MsalV3CacheFileName); if (adalv3State != null) { args.TokenCache.DeserializeAdalV3(adalv3State); } if (msalv2State != null) { #pragma warning disable CS0618 // Type or member is obsolete args.TokenCache.DeserializeMsalV2(msalv2State); #pragma warning restore CS0618 // Type or member is obsolete } if (msalv3State != null) { args.TokenCache.DeserializeMsalV3(msalv3State); } } }