Exemplo n.º 1
0
        // 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)
            {
                // check for an existing entry
                Cache = db.PerUserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
                if (Cache == null)
                {
                    // if no existing entry for that user, create a new one
                    Cache = new PerUserTokenCache
                    {
                        webUserUniqueId = User,
                    };
                }

                // update the cache contents and the last write timestamp
                Cache.cacheBits = this.Serialize();
                Cache.LastWrite = DateTime.Now;

                // update the DB with modification or new entry
                db.Entry(Cache).State = Cache.Id == 0 ? EntityState.Added : EntityState.Modified;
                db.SaveChanges();
                this.HasStateChanged = false;
            }
        }
Exemplo n.º 2
0
        // 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)
        {
            using (DataAccess db = new DataAccess()) {
                if (_cache == null)
                {
                    // first time access
                    _cache = db.PerUserTokenCacheList.AsNoTracking().FirstOrDefault(c => c.WebUserUniqueId == _user);
                }
                else                       // retrieve last write from the DB
                {
                    var status =
                        from e in db.PerUserTokenCacheList
                        where (e.WebUserUniqueId == _user)
                        select new { LastWrite = e.LastWrite };
                    // if the in-memory copy is older than the persistent copy
                    if (status.AsNoTracking().First().LastWrite > _cache.LastWrite)
                    {
                        //// read from from storage, update in-memory copy
                        _cache = db.PerUserTokenCacheList.AsNoTracking().FirstOrDefault(c => c.WebUserUniqueId == _user);
                    }
                }
            }

            this.Deserialize((_cache == null) ? null : _cache.CacheBits);
        }
Exemplo n.º 3
0
        // constructor
        public ADALTokenCache(string user)
        {
            // associate the cache to the current user of the web app
            User = user;

            this.AfterAccess  = AfterAccessNotification;
            this.BeforeAccess = BeforeAccessNotification;
            this.BeforeWrite  = BeforeWriteNotification;

            // look up the entry in the DB
            Cache = db.PerUserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);

            // place the entry in memory
            this.Deserialize((Cache == null) ? null : Cache.cacheBits);
        }
Exemplo n.º 4
0
        // constructor
        public AdalTokenCache(string user)
        {
            // associate the cache to the current user of the web app
            _user = user;

            this.AfterAccess  = AfterAccessNotification;
            this.BeforeAccess = BeforeAccessNotification;
            this.BeforeWrite  = BeforeWriteNotification;

            using (DataAccess db = new DataAccess()) {
                // look up the entry in the DB
                _cache = db.PerUserTokenCacheList.AsNoTracking().FirstOrDefault(c => c.WebUserUniqueId == _user);

                // place the entry in memory
                this.Deserialize((_cache == null) ? null : _cache.CacheBits);
            }
        }
Exemplo n.º 5
0
 // 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 = db.PerUserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
     }
     else
     {   // retrieve last write from the DB
         var status = from e in db.PerUserTokenCacheList
                      where (e.webUserUniqueId == User)
                      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 = db.PerUserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
         }
     }
     this.Deserialize((Cache == null) ? null : Cache.cacheBits);
 }