protected override async Task WriteCacheBytesAsync(string cacheKey, byte[] bytes)
        {
            if (_cacheDbRecord == null)
            {
                _cacheDbRecord = new TokenCacheDbRecord
                {
                    CacheKey = cacheKey
                };
            }

            _cacheDbRecord.CacheBits = _dataProtector.Protect(bytes);
            _cacheDbRecord.LastWrite = DateTime.Now;

            try
            {
                // Update the DB and the lastwrite
                _tokenCacheDb.Entry(_cacheDbRecord).State = _cacheDbRecord.TokenCacheId == 0 ? EntityState.Added : EntityState.Modified;
                _tokenCacheDb.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                // Record already updated on a different thread, so just read the updated record
                await ReadCacheBytesAsync(cacheKey).ConfigureAwait(false);
            }
        }
示例#2
0
        protected override async Task <byte[]> ReadCacheBytesAsync(string cacheKey)
        {
            if (_inMemoryCache == null) // first time access
            {
                try
                {
                    _inMemoryCache = GetLatestRecordQuery(cacheKey).FirstOrDefault();
                }
                catch (SqlException ex) when(ex.Message == "Invalid object name 'Records'")
                {
                    // Microsoft.Identity.Web changed from several tables (AppTokenCache, UserTokenCache) to one table (record)
                    // If you care about the cache, you can migrate them, otherwise re-create the database
                    throw new Exception("You need to migrate the AppTokenCache and UserTokenCache tables to Records, or run SqlTokenCacheProviderExtension.CreateTokenCachingTablesInSqlDatabase() to create the database", ex);
                }
                catch (SqlException ex) when(ex.Message.StartsWith("Cannot open database \"MY_TOKEN_CACHE_DATABASE\" requested by the login."))
                {
                    throw new Exception("You need to run SqlTokenCacheProviderExtension.CreateTokenCachingTablesInSqlDatabase() to create the database", ex);
                }
            }
            else
            {
                // retrieve last written record from the DB
                var lastwriteInDb = GetLatestRecordQuery(cacheKey).Select(n => n.LastWrite).FirstOrDefault();

                // if the persisted copy is newer than the in-memory copy
                if (lastwriteInDb > _inMemoryCache.LastWrite)
                {
                    // read from from storage, update in-memory copy
                    _inMemoryCache = GetLatestRecordQuery(cacheKey).FirstOrDefault();
                }
            }

            // Send data to the TokenCache instance
            return((_inMemoryCache == null) ? null : _dataProtector.Unprotect(_inMemoryCache.CacheBits));
        }
        protected override Task <byte[]> ReadCacheBytesAsync(string cacheKey)
        {
            try
            {
                _cacheDbRecord = GetLatestRecordQuery(cacheKey).FirstOrDefault();
            }
            catch (SqlException ex) when(ex.Message == "Invalid object name 'Records'")
            {
                // Microsoft.Identity.Web changed from several tables (AppTokenCache, UserTokenCache) to one table (record)
                // If you care about the cache, you can migrate them, otherwise re-create the database
                throw new Exception("You need to migrate the AppTokenCache and UserTokenCache tables to Records, or run SqlTokenCacheProviderExtension.CreateTokenCachingTablesInSqlDatabase() to create the database", ex);
            }
            catch (SqlException ex) when(ex.Message.StartsWith("Cannot open database"))
            {
                throw new Exception("You need to run SqlTokenCacheProviderExtension.CreateTokenCachingTablesInSqlDatabase() to create the database", ex);
            }

            // Send data to the TokenCache instance
            return(Task.FromResult((_cacheDbRecord == null) ? null : _dataProtector.Unprotect(_cacheDbRecord.CacheBits)));
        }