Beispiel #1
0
        // clean up the DB
        public override void Clear()
        {
            base.Clear();
            IEnumerable <PerWebUserCache> entries = null;
            var task = Task.Run(async() => {
                entries = await PerWebUserCache.GetAllEntries();
            });

            task.Wait();

            foreach (var cacheEntry in entries)
            {
                PerWebUserCache.RemoveEntry(cacheEntry).Wait();
            }
        }
        // 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;
            this.BeforeWrite  = BeforeWriteNotification;

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

            task.Wait();

            // place the entry in memory
            this.Deserialize((Cache == null) ? null : B2BPortal.Common.Utils.Utils.Decrypt(new EncryptedObj(Cache.CacheBits, Cache.Salt)));
        }
Beispiel #3
0
        // 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 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 : B2BPortal.Common.Utils.Utils.Decrypt(new EncryptedObj(Cache.CacheBits, Cache.Salt)));
        }
Beispiel #4
0
        // Notification raised after ADAL accessed the cache.
        // If the HasStateChanged flag is set, ADAL changed the content of the cache
        void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            Task task;

            // if state changed
            if (this.HasStateChanged)
            {
                var enc = B2BPortal.Common.Utils.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
                    task = Task.Run(async() => {
                        await PerWebUserCache.UpdateEntry(Cache);
                    });
                    task.Wait();
                }
                else
                {
                    Cache = new PerWebUserCache
                    {
                        WebUserUniqueId = _userObjId,
                        CacheBits       = enc.EncryptedData,
                        Salt            = enc.VectorData,
                        LastWrite       = DateTime.Now,
                        HostName        = _hostName
                    };
                    // add the entry
                    task = Task.Run(async() => {
                        await PerWebUserCache.AddEntry(Cache);
                    });
                    task.Wait();
                }

                this.HasStateChanged = false;
            }
        }
Beispiel #5
0
        // 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;
            this.BeforeWrite  = BeforeWriteNotification;

            // 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 : B2BPortal.Common.Utils.Utils.Decrypt(new EncryptedObj(Cache.CacheBits, Cache.Salt)));
            }
            catch (CryptographicException)
            {
                //error decrypting from token cache - clearing the cached item (encryption key may have changed)
                task = Task.Run(async() => {
                    await PerWebUserCache.RemoveEntry(Cache);
                });
                task.Wait();
                this.Deserialize(null);
            }
            catch (Exception ex)
            {
                var newEx = new Exception("Error decrypting the cached token. ", ex);
                throw newEx;
            }
        }
Beispiel #6
0
 public static async Task RemoveEntry(PerWebUserCache cache)
 {
     await DocDBRepo.DB <PerWebUserCache> .DeleteItemAsync(cache);
 }
Beispiel #7
0
 public static async Task <PerWebUserCache> UpdateEntry(PerWebUserCache cache)
 {
     return(await DocDBRepo.DB <PerWebUserCache> .UpdateItemAsync(cache));
 }