// 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 #2
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 #3
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;
            }
        }