// Notification raised after ADAL accessed the cache.
        // If the HasStateChanged flag is set, ADAL changed the content of the cache
        async void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            //Task task;
            // if state changed
            if (this.HasStateChanged)
            {
                var enc = Utils.Encrypt(this.Serialize());

                if (Cache != null)
                {
                    Cache.CacheBits = enc.EncryptedData;
                    Cache.Salt      = enc.VectorData;
                    Cache.LastWrite = DateTime.Now;
                    // update the DB and the lastwrite
                    await PerWebUserCache.UpdateEntry(Cache);
                }
                else
                {
                    Cache = new PerWebUserCache
                    {
                        WebUserUniqueId = _userObjId,
                        CacheBits       = enc.EncryptedData,
                        Salt            = enc.VectorData,
                        LastWrite       = DateTime.Now,
                        HostName        = _hostName
                    };
                    await PerWebUserCache.AddEntry(Cache);
                }

                this.HasStateChanged = false;
            }
        }
        // Notification raised before ADAL accesses the cache.
        // This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
        async void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            if (Cache == null)
            {
                // first time access
                Cache = await PerWebUserCache.GetCache(new CacheUser(_userObjId, _hostName));
            }
            else
            {
                // retrieve last write from the DB
                var dbCache = await PerWebUserCache.GetCache(new CacheUser(_userObjId, _hostName));

                if (dbCache == null)
                {
                    Cache = await PerWebUserCache.GetCache(new CacheUser(_userObjId, _hostName));
                }
                else
                {
                    // if the in-memory copy is older than the persistent copy
                    if (dbCache.LastWrite > Cache.LastWrite)
                    {
                        // update in-memory copy
                        Cache = dbCache;
                    }
                }
            }
            this.Deserialize((Cache == null) ? null : Utils.Decrypt(new EncryptedObj(Cache.CacheBits, Cache.Salt)));
        }
        // constructor
        public AdalCosmosTokenCache(string userObjId, string hostName)
        {
            // associate the cache to the current user of the web app
            _userObjId = userObjId;
            _hostName  = hostName;

            this.AfterAccess  = AfterAccessNotification;
            this.BeforeAccess = BeforeAccessNotification;

            // look up the entry in the DB
            var task = Task.Run(async() => {
                Cache = await PerWebUserCache.GetCache(new CacheUser(_userObjId, _hostName));
            });

            task.Wait();

            try
            {
                // place the entry in memory
                this.Deserialize((Cache == null) ? null : Utils.Decrypt(new EncryptedObj(Cache.CacheBits, Cache.Salt)));
            }
            catch (CryptographicException)
            {
                //token is invalid for decryption - delete it and start fresh
                DeleteItem(Cache);
                this.Deserialize(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void DeleteItem(PerWebUserCache cache)
        {
            var task = Task.Run(async() => {
                await PerWebUserCache.RemoveEntry(cache);
            });

            task.Wait();
        }
        /// <summary>
        /// Remove all ADAL cache entries from the database
        /// </summary>
        /// <returns></returns>
        public static async Task FlushAllCache()
        {
            IEnumerable <PerWebUserCache> entries = null;

            entries = await PerWebUserCache.GetAllEntries();

            foreach (var cacheEntry in entries)
            {
                await PerWebUserCache.RemoveEntry(cacheEntry);
            }
        }
 public static async Task RemoveEntry(PerWebUserCache cache)
 {
     await DocDBRepo.DB <PerWebUserCache> .DeleteItemAsync(cache).ConfigureAwait(false);
 }
 public static async Task <PerWebUserCache> UpdateEntry(PerWebUserCache cache)
 {
     return(await DocDBRepo.DB <PerWebUserCache> .UpdateItemAsync(cache).ConfigureAwait(false));
 }
 public static async Task DeleteItem(HttpContextBase hctx)
 {
     await PerWebUserCache.RemoveEntry(new CacheUser(hctx));
 }