// 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 void BeforeAccessNotification(TokenCacheNotificationArgs args) { if (_cache == null) { // first time access _cache = _memCache.FirstOrDefault(c => c.webUserUniqueId == _userId); this.HasStateChanged = true; // Fake DB } else { // retrieve last write from the db var status = from e in _memCache where (e.webUserUniqueId == _userId) select new { LastWrite = e.LastWrite }; // if the in-memory copy is older than the persistent copy if (status.First().LastWrite > _cache.LastWrite) //// read from from storage, update in-memory copy { _cache = _memCache.FirstOrDefault(c => c.webUserUniqueId == _userId); } } this.Deserialize((_cache == null) ? null : _cache.cacheBits); }
// Notification raised after ADAL accessed the cache. // If the HasStateChanged flag is set, ADAL changed the content of the cache void AfterAccessNotification(TokenCacheNotificationArgs args) { // if state changed if (this.HasStateChanged) { _cache = new PerWebUserCache { webUserUniqueId = _userId, cacheBits = this.Serialize(), LastWrite = DateTime.Now }; //// update the db and the lastwrite _db.Entry(_cache).State = _cache.EntryId == 0 ? EntityState.Added : EntityState.Modified; _db.SaveChanges(); this.HasStateChanged = false; } }