Пример #1
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 = _Context.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
            }
            else
            {   // retrieve last write from the DB
                var status = from e in _Context.PerUserCacheList
                             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 = _Context.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
                }
            }


            this.Deserialize((Cache == null) ? null : Cache.cacheBits);
        }
    // constructor
    public EFADALTokenCache(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.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
      // place the entry in memory
      this.Deserialize((Cache == null) ? null : Cache.cacheBits);
    }
Пример #3
0
        // constructor
        public EFADALTokenCache(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 = _Context.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
            // place the entry in memory
            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 = User,
       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;
   }
 }
 // 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 = User,
             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;
     }
 }
 // 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 = GetPerUserCache(User);
         //Cache = db.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
     }
     else
     {   // retrieve last write from the DB
         PerWebUserCache cache = GetPerUserCache(User);
         // if the in-memory copy is older than the persistent copy
         if (cache.LastWrite > Cache.LastWrite)
         //// read from from storage, update in-memory copy
         {
             Cache = cache;
         }
     }
     this.Deserialize((Cache == null) ? null : Cache.cacheBits);
 }
Пример #7
0
        //public void SaveUserStateValue(string state)
        //{
        //    SessionLock.EnterWriteLock();
        //    //httpContext.Session[CacheId + "_state"] = state;

        //    //===============EF==================

        //    // Get userCache from DB
        //    PerWebUserCache userCache = _context.PerWebUserCaches.FirstOrDefault(c => c.WebUserUniqueId == UserId);

        //    // Update existing record
        //    if (userCache != null)
        //    {
        //        userCache.UserState = state;
        //    }
        //    // Create a new record
        //    else
        //    {
        //        userCache = new PerWebUserCache()
        //        {
        //            WebUserUniqueId = UserId,
        //            UserState = state
        //        };
        //    }

        //    // Update the cache to DB
        //    _context.Entry(userCache).State = userCache.Id == 0 ? EntityState.Added : EntityState.Modified;
        //    _context.SaveChanges();

        //    // =====================================

        //    SessionLock.ExitWriteLock();
        //}
        //public string ReadUserStateValue()
        //{
        //    string state = string.Empty;
        //    SessionLock.EnterReadLock();
        //    //state = (string)httpContext.Session[CacheId + "_state"];

        //    //===============EF==================

        //    // Get userCache from DB
        //    PerWebUserCache userCache = _context.PerWebUserCaches.FirstOrDefault(c => c.WebUserUniqueId == UserId);

        //    // Update existing record
        //    if (userCache != null)
        //    {
        //        state = userCache.UserState;
        //    }


        //    // =====================================


        //    SessionLock.ExitReadLock();
        //    return state;
        //}
        public void Load()
        {
            SessionLock.EnterReadLock();
            //cache.Deserialize((byte[])httpContext.Session[CacheId]);

            //===============EF==================

            // Get userCache from DB
            PerWebUserCache userCache = _context.PerWebUserCaches.FirstOrDefault(c => c.WebUserUniqueId.Equals(UserId));

            // Update existing record
            if (userCache != null)
            {
                cache.Deserialize(userCache.CacheBits);
            }


            // =====================================

            SessionLock.ExitReadLock();
        }
    // 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.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
      } else {   // retrieve last write from the DB
        var status = from e in db.PerUserCacheList
                     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.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
        }
      }


      this.Deserialize((Cache == null) ? null : Cache.cacheBits);
    }
Пример #9
0
        public void Persist()
        {
            SessionLock.EnterWriteLock();

            // Optimistically set HasStateChanged to false. We need to do it early to avoid losing changes made by a concurrent thread.
            cache.HasStateChanged = false;

            // Reflect changes in the persistent store
            //httpContext.Session[CacheId] = cache.Serialize();

            //===============EF==================

            // Get userCache from DB
            PerWebUserCache userCache = _context.PerWebUserCaches.FirstOrDefault(c => c.WebUserUniqueId.Equals(UserId));

            // Update existing record
            if (userCache != null)
            {
                userCache.CacheBits = cache.Serialize();
                userCache.LastWrite = DateTime.Now;
            }
            // Create a new record
            else
            {
                userCache = new PerWebUserCache()
                {
                    WebUserUniqueId = UserId,
                    CacheBits       = cache.Serialize(),
                    LastWrite       = DateTime.Now
                };
            }

            // Update the cache to DB
            _context.Entry(userCache).State = userCache.Id == 0 ? EntityState.Added : EntityState.Modified;
            _context.SaveChanges();

            // =====================================

            SessionLock.ExitWriteLock();
        }
        // 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 = User,
                    cacheBits = this.Serialize(),
                    LastWrite = DateTime.Now
                };

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbTokenCache"].ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd;
                    if (Cache.EntryId == 0)
                    {
                        cmd = new SqlCommand("INSERT INTO PerWebUserCaches (webUserUniqueId, cacheBits, LastWrite) VALUES (@webUserUniqueId, @cacheBits, @LastWrite)", conn);
                        cmd.Parameters.AddWithValue("@webUserUniqueId", Cache.webUserUniqueId);
                        cmd.Parameters.AddWithValue("@cacheBits", Cache.cacheBits);
                        cmd.Parameters.AddWithValue("@LastWrite", Cache.LastWrite);
                    }
                    else
                    {
                        cmd = new SqlCommand("UPDATE PerWebUserCaches SET webUserUniqueId = @webUserUniqueId, cacheBits = @cacheBits, LastWrite = @LastWrite WHERE EntryId = @EntryId", conn);
                        cmd.Parameters.AddWithValue("@webUserUniqueId", Cache.webUserUniqueId);
                        cmd.Parameters.AddWithValue("@cacheBits", Cache.cacheBits);
                        cmd.Parameters.AddWithValue("@LastWrite", Cache.LastWrite);
                        cmd.Parameters.AddWithValue("@EntryId", Cache.EntryId);
                    }
                    cmd.CommandType = System.Data.CommandType.Text;

                    using (cmd)
                    {
                        cmd.ExecuteNonQuery();
                    }

                }

                this.HasStateChanged = false;
            }
        }
        PerWebUserCache GetPerUserCache(string user)
        {
            PerWebUserCache cache = null;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbTokenCache"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT EntryId, webUserUniqueId, cacheBits, LastWrite FROM PerWebUserCaches WHERE webUserUniqueId = @webUserUniqueId", conn))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.AddWithValue("@webUserUniqueId", user);

                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.Read())
                    {
                        cache = new PerWebUserCache
                        {
                            EntryId = (int)rdr["EntryId"],
                            webUserUniqueId = rdr["webUserUniqueId"].ToString(),
                            cacheBits = (byte[])rdr["cacheBits"],
                            LastWrite = (DateTime)rdr["LastWrite"]
                        };
                    }
                }
            }

            return cache;
        }