Example #1
0
        /// <summary>
        /// Persists the user token blob to the Http session.
        /// </summary>
        private void PersistUserTokenCache()
        {
            string cacheKey = GetSignedInUsersUniqueId();

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

            s_sessionLock.EnterWriteLock();

            try
            {
                Debug.WriteLine($"INFO: Serializing session {_httpContext.Session.Id}, cacheId {cacheKey}");

                // Reflect changes in the persistent store
                byte[] blob = _userTokenCache.SerializeMsalV3();
                _httpContext.Session.Set(cacheKey, blob);
                _httpContext.Session.CommitAsync().Wait();
            }
            finally
            {
                s_sessionLock.ExitWriteLock();
            }
        }
Example #2
0
        /// <summary>
        /// Raised AFTER MSAL added the new token in its in-memory copy of the cache.
        /// This notification is called every time MSAL accessed the cache, not just when a write took place:
        /// If MSAL's current operation resulted in a cache change, the property TokenCacheNotificationArgs.HasStateChanged will be set to true.
        /// If that is the case, we call the TokenCache.Serialize() to get a binary blob representing the latest cache content – and persist it.
        /// </summary>
        /// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
        private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs args)
        {
            SetSignedInUserFromNotificationArgs(args);

            // if state changed, i.e. new token obtained
            if (args.HasStateChanged && !string.IsNullOrWhiteSpace(GetSignedInUsersUniqueId()))
            {
                if (_inMemoryCache == null)
                {
                    _inMemoryCache = new UserTokenCache
                    {
                        WebUserUniqueId = GetSignedInUsersUniqueId()
                    };
                }

                _inMemoryCache.CacheBits = _dataProtector.Protect(_userTokenCache.SerializeMsalV3());
                _inMemoryCache.LastWrite = DateTime.Now;

                try
                {
                    // Update the DB and the lastwrite
                    _tokenCacheDb.Entry(_inMemoryCache).State = _inMemoryCache.UserTokenCacheId == 0
                        ? EntityState.Added
                        : EntityState.Modified;

                    _tokenCacheDb.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    // Record already updated on a different thread, so just read the updated record
                    ReadCacheForSignedInUser();
                }
            }
        }
Example #3
0
        public void Persist()
        {
            SessionLock.EnterWriteLock();

            // Reflect changes in the persistent store
            httpContext.Session.Set(CacheId, cache.SerializeMsalV3());
            SessionLock.ExitWriteLock();
        }
        public void Persist()
        {
            SessionLock.EnterWriteLock();

            // Reflect changes in the persistent store
            staticCache[CacheId] = cache.SerializeMsalV3();
            SessionLock.ExitWriteLock();
        }
        /// <summary>
        /// Persists the user token blob to the memoryCache.
        /// </summary>
        private void PersistUserTokenCache()
        {
            string cacheKey = GetMsalAccountId();

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

            // Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
            memoryCache.Set(GetMsalAccountId(), UserTokenCache.SerializeMsalV3(), cacheDuration);
        }
Example #6
0
        /// <summary>
        /// Persists the application token's to session cache.
        /// </summary>
        private void PersistAppTokenCache()
        {
            s_sessionLock.EnterWriteLock();

            try
            {
                Debug.WriteLine($"INFO: Serializing session {_httpContext.Session.Id}, cacheId {_appCacheId}");

                // Reflect changes in the persistent store
                byte[] blob = _apptokenCache.SerializeMsalV3();
                _httpContext.Session.Set(_appCacheId, blob);
                _httpContext.Session.CommitAsync().Wait();
            }
            finally
            {
                s_sessionLock.ExitWriteLock();
            }
        }
        public void Persist()
        {
            SessionLock.EnterWriteLock();

            try
            {
                Debug.WriteLine($"INFO: Serializing session {session.Id}, cacheId {CacheId}");

                // Reflect changes in the persistent store
                byte[] blob = cache.SerializeMsalV3();
                session.Set(CacheId, blob);
                session.CommitAsync().Wait();
            }
            finally
            {
                SessionLock.ExitWriteLock();
            }
        }
Example #8
0
 /// <summary>
 /// Persists the application's token to the cache.
 /// </summary>
 private void PersistAppTokenCache()
 {
     // Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
     // Reflect changes in the persistence store
     memoryCache.Set(AppCacheId, AppTokenCache.SerializeMsalV3(), cacheDuration);
 }
 public void Persist()
 {
     // Reflect changes in the persistent store
     byte[] blob = cache.SerializeMsalV3();
     memoryCache.Set(CacheId, blob);
 }
Example #10
0
 private void Persist()
 {
     sessionLock.EnterReadLock();
     httpContext.Session[cacheId] = tokenCache.SerializeMsalV3();
     sessionLock.ExitReadLock();
 }
Example #11
0
 private void Persist(string key, ITokenCache tokenCache)
 {
     _cache.Set(key, tokenCache.SerializeMsalV3(), DateTimeOffset.Now.AddHours(12));
 }
Example #12
0
 /// <summary>
 /// Persists the user token blob to the memoryCache.
 /// </summary>
 private void PersistUserTokenCache()
 {
     // Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
     _memoryCache.Set(GetMsalAccountId(), _userTokenCache.SerializeMsalV3(), _cacheOptions.AbsoluteExpiration);
 }