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(); } }
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); } } }
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(); }
internal TokenCacheNotificationArgs( ITokenCacheSerializer tokenCacheSerializer, string clientId, IAccount account, bool hasStateChanged) { TokenCache = tokenCacheSerializer; ClientId = clientId; Account = account; HasStateChanged = hasStateChanged; }
public void Load(ITokenCacheSerializer cacheSerializer) { // Retrieve any existing tokens from session state. // Locks are used to ensure thread safety. SessionLock.EnterReadLock(); byte[] blob = httpContext.Session.Get(CacheId); if (blob != null) { cacheSerializer.DeserializeMsalV3(blob); } SessionLock.ExitReadLock(); }
public void Load(ITokenCacheSerializer tokenCacheSerializer) { TokenCacheLock.EnterReadLock(); var sessionValue = _context.Session.Get(_sessionKey); if (sessionValue != null) { tokenCacheSerializer.DeserializeMsalV3(sessionValue); } TokenCacheLock.ExitReadLock(); }
/// <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); }
private void Load(string key, ITokenCacheSerializer tokenCache) { using IServiceScope scope = serviceProvider.CreateScope(); IMsalTokenDbContext ctx = scope.ServiceProvider.GetRequiredService <IMsalTokenDbContext>(); MsalToken token = ctx.MsalTokens.Find(key); if (token != null) { tokenCache.DeserializeMsalV3(token.Value); } }
/// <summary> /// Loads the user token cache from memory. /// </summary> private void LoadUserTokenCacheFromMemory(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. byte[] tokenCacheBytes = (byte[])this.memoryCache.Get(this.GetMsalAccountId()); tokenCache.DeserializeMsalV3(tokenCacheBytes); }
internal TokenCacheNotificationArgs( ITokenCacheSerializer tokenCacheSerializer, string clientId, IAccount account, bool hasStateChanged, bool isAppCache, bool hasTokens, string suggestedCacheKey = null) { TokenCache = tokenCacheSerializer; ClientId = clientId; Account = account; HasStateChanged = hasStateChanged; IsApplicationCache = isAppCache; HasTokens = hasTokens; SuggestedCacheKey = suggestedCacheKey; }
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(); }
internal TokenCacheNotificationArgs( ITokenCacheSerializer tokenCacheSerializer, string clientId, IAccount account, bool hasStateChanged, bool isAppCache, bool hasTokens, CancellationToken cancellationToken, string suggestedCacheKey = null, DateTimeOffset?suggestedCacheExpiry = null) { TokenCache = tokenCacheSerializer; ClientId = clientId; Account = account; HasStateChanged = hasStateChanged; IsApplicationCache = isAppCache; HasTokens = hasTokens; SuggestedCacheKey = suggestedCacheKey; CancellationToken = cancellationToken; SuggestedCacheExpiry = suggestedCacheExpiry; }
/// <summary> /// This constructor is for test purposes only. It allows apps to unit test their MSAL token cache implementation code. /// </summary> public TokenCacheNotificationArgs( ITokenCacheSerializer tokenCache, string clientId, IAccount account, bool hasStateChanged, bool isApplicationCache, string suggestedCacheKey, bool hasTokens, DateTimeOffset?suggestedCacheExpiry, CancellationToken cancellationToken) : this(tokenCache, clientId, account, hasStateChanged, isApplicationCache, suggestedCacheKey, hasTokens, suggestedCacheExpiry, cancellationToken, default) { }
/// <summary> /// Reads the cache data from the backend database. /// </summary> private void ReadCacheForSignedInApp(ITokenCacheSerializer tokenCache) { if (_inMemoryCache == null) // first time access { _inMemoryCache = GetLatestAppRecordQuery().FirstOrDefault(); } else { // retrieve last written record from the DB var lastwriteInDb = GetLatestAppRecordQuery().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 = GetLatestAppRecordQuery().FirstOrDefault(); } } // Send data to the TokenCache instance tokenCache.DeserializeMsalV3((_inMemoryCache == null) ? null : _dataProtector.Unprotect(_inMemoryCache.CacheBits)); }
/// <summary> /// This constructor is for test purposes only. It allows apps to unit test their MSAL token cache implementation code. /// </summary> public TokenCacheNotificationArgs( ITokenCacheSerializer tokenCache, string clientId, IAccount account, bool hasStateChanged, bool isApplicationCache, string suggestedCacheKey, bool hasTokens, DateTimeOffset?suggestedCacheExpiry, CancellationToken cancellationToken, Guid correlationId) { TokenCache = tokenCache; ClientId = clientId; Account = account; HasStateChanged = hasStateChanged; IsApplicationCache = isApplicationCache; SuggestedCacheKey = suggestedCacheKey; HasTokens = hasTokens; CancellationToken = cancellationToken; CorrelationId = correlationId; SuggestedCacheExpiry = suggestedCacheExpiry; }
private static TokenCacheNotificationArgs InstantiateTokenCacheNotificationArgs(TestTokenCache tokenCache) { ITokenCacheSerializer tokenCacheSerializer = tokenCache; string clientId = string.Empty; IAccount account = null; bool hasStateChanged = true; bool isAppCache = false; bool hasTokens = true; CancellationToken cancellationToken = CancellationToken.None; string suggestedCacheKey = "key"; DateTimeOffset? suggestedCacheExpiry = null; TokenCacheNotificationArgs args = new TokenCacheNotificationArgs( tokenCacheSerializer, clientId, account, hasStateChanged, isAppCache, suggestedCacheKey, hasTokens, suggestedCacheExpiry, cancellationToken); return(args); }
private void Load(string key, ITokenCacheSerializer tokenCache) { var data = (byte[])cache.Get(key); tokenCache.DeserializeMsalV3(data); }
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(); }