コード例 #1
0
        private async Task UpdateCacheDataAsync(ITokenCacheSerializer tokenCache)
        {
            await _lock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (!_cacheAccessMap.TryGetValue(tokenCache, out CacheTimestamp lastRead) || lastRead.Value < _lastUpdated)
                {
                    Data = await MergeCacheData(Data, tokenCache.SerializeMsalV3()).ConfigureAwait(false);
                }
                else
                {
                    Data = tokenCache.SerializeMsalV3();
                }

                if (TokenCacheUpdatedAsync != null)
                {
                    var eventBytes = Data.ToArray();
                    await TokenCacheUpdatedAsync(new TokenCacheUpdatedArgs(eventBytes)).ConfigureAwait(false);
                }

                _lastUpdated = _cacheAccessMap.GetOrCreateValue(tokenCache).Update();
            }
            finally
            {
                _lock.Release();
            }
        }
コード例 #2
0
        private async Task UpdateCacheDataAsync(ITokenCacheSerializer tokenCache)
        {
            await _lock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (!_cacheAccessMap.TryGetValue(tokenCache, out CacheTimestamp lastRead) || lastRead.Value < _lastUpdated)
                {
                    _data = await MergeCacheData(_data, tokenCache.SerializeMsalV3()).ConfigureAwait(false);
                }
                else
                {
                    _data = tokenCache.SerializeMsalV3();
                }

                _cacheAccessMap.GetOrCreateValue(tokenCache).Update();

                _lastUpdated = DateTime.UtcNow;
            }
            finally
            {
                _lock.Release();
            }

            if (Updated != null)
            {
                foreach (Func <TokenCacheUpdatedArgs, Task> handler in Updated.GetInvocationList())
                {
                    await handler(new TokenCacheUpdatedArgs(this)).ConfigureAwait(false);
                }
            }
        }
コード例 #3
0
        public void Persist(ITokenCacheSerializer cacheSerializer)
        {
            // Write the tokens to session state.
            // Locks are used to ensure thread safety.
            SessionLock.EnterWriteLock();

            // Reflect changes in the persistent store
            httpContext.Session.Set(CacheId, cacheSerializer.SerializeMsalV3());
            SessionLock.ExitWriteLock();
        }
コード例 #4
0
        /// <summary>
        /// Persists the user token blob to the memoryCache.
        /// </summary>
        private void PersistUserTokenCache(ITokenCacheSerializer tokenCache)
        {
            string cacheKey = this.GetMsalAccountId();

            if (string.IsNullOrWhiteSpace(cacheKey))
            {
                return;
            }

            // Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
            this.memoryCache.Set(this.GetMsalAccountId(), tokenCache.SerializeMsalV3(), this.cacheDuration);
        }
コード例 #5
0
        private void Persist(string key, ITokenCacheSerializer tokenCache)
        {
            using IServiceScope scope = serviceProvider.CreateScope();
            IMsalTokenDbContext ctx = scope.ServiceProvider.GetRequiredService <IMsalTokenDbContext>();

            var       value = tokenCache.SerializeMsalV3();
            MsalToken token = ctx.MsalTokens.Find(key);

            if (token != null)
            {
                token.Value = value;
            }
            else
            {
                ctx.MsalTokens.Add(new MsalToken {
                    Id = key, Value = value
                });
            }

            ctx.SaveChanges();
        }
コード例 #6
0
 private void Persist(string key, ITokenCacheSerializer tokenCache)
 {
     cache.Set(key, tokenCache.SerializeMsalV3(), DateTimeOffset.Now.AddHours(12));
 }
 public void Save(ITokenCacheSerializer tokenCacheSerializer)
 {
     TokenCacheLock.EnterWriteLock();
     _context.Session.Set(_sessionKey, tokenCacheSerializer.SerializeMsalV3());
     TokenCacheLock.ExitWriteLock();
 }