/// <summary>
        ///     This is called when the TokenCache is set into the clientapplicationbase and we need to
        ///     hook it up since the storage manager is the actual source of truth for the cache...
        /// </summary>
        /// <param name="storageManager"></param>
        internal void BindToStorageManager(IStorageManager storageManager)
        {
            if (storageManager == null)
            {
                throw new ArgumentNullException(nameof(storageManager));
            }

            lock (_lock)
            {
                _storageManager = storageManager;

                // If the user has deserialized any data into the cache before binding...
                if (_deserializedCacheDataHolding.UnifiedState != null && _deserializedCacheDataHolding.UnifiedState.Any())
                {
                    // decode buffer and translate data into the storage manager
                    _storageManager.Deserialize(_deserializedCacheDataHolding.UnifiedState);
                }

                if (_deserializedCacheDataHolding.AdalV3State != null && _deserializedCacheDataHolding.AdalV3State.Any())
                {
                    // decode buffer and translate into the adal legacy cache manager
                    LegacyCachePersistence.WriteCache(_deserializedCacheDataHolding.AdalV3State);
                }

                // subscribe to IStorageManager beforeaccess/afteraccess/beforewrite events so we can forward them as needed to the caller...
                //_storageManager.BeforeAccess += StorageManagerOnBeforeAccess;
                //_storageManager.BeforeWrite += StorageManagerOnBeforeWrite;
                //_storageManager.AfterAccess += StorageManagerOnAfterAccess;
            }
        }
        public void DeserializeUnifiedAndAdalCache(CacheData cacheData)
        {
            lock (_lock)
            {
                if (_storageManager == null && LegacyCachePersistence == null)
                {
                    _deserializedCacheDataHolding = cacheData;
                }

                _storageManager?.Deserialize(cacheData.UnifiedState);
                LegacyCachePersistence?.WriteCache(cacheData.AdalV3State);
            }
        }
 public void Deserialize(byte[] unifiedState)
 {
     lock (_lock)
     {
         if (_storageManager == null)
         {
             _deserializedCacheDataHolding.UnifiedState = new byte[unifiedState.Length];
             Array.Copy(unifiedState, _deserializedCacheDataHolding.UnifiedState, unifiedState.Length);
         }
         else
         {
             // call into _storageManager and push the contents...
             _storageManager.Deserialize(unifiedState);
         }
     }
 }