// 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) { if (Cache == null) { Cache = new UserTokenCache { webUserUniqueId = userId }; } Cache.cacheBits = MachineKey.Protect(this.Serialize(), "ADALCache"); Cache.LastWrite = DateTime.Now; // update the DB and the lastwrite db.Entry(Cache).State = Cache.UserTokenCacheId == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); this.HasStateChanged = false; } }