Ejemplo n.º 1
0
        public RedisTokenCache(string signedInUserId)
        {
            // associate the cache to the current user of the web app
            userId            = signedInUserId;
            this.AfterAccess  = AfterAccessNotification;
            this.BeforeAccess = BeforeAccessNotification;
            this.BeforeWrite  = BeforeWriteNotification;
            // look up the entry in the cache
            var cache = Redis.Connection.GetDatabase();

            try
            {
                var cachedItem = cache.StringGet(userId);
                if (cachedItem.HasValue)
                {
                    Cache = JsonConvert.DeserializeObject <UserTokenCacheItem>(cachedItem);
                    this.Deserialize(Cache?.cacheBits);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Exception in RedisTokenCache(id): " + ex.Message);
                Cache = null;
            }
        }
Ejemplo n.º 2
0
 // Notification raised before ADAL accesses the cache.
 // This is your chance to update the in-memory copy from the cache, if the in-memory version is stale
 void BeforeAccessNotification(TokenCacheNotificationArgs args)
 {
     try
     {
         var cache      = Redis.Connection.GetDatabase();
         var cachedItem = cache.StringGet(userId);
         if (cachedItem.HasValue)
         {
             var status = JsonConvert.DeserializeObject <UserTokenCacheItem>(cachedItem);
             if ((Cache != null) && (status.LastWrite > Cache.LastWrite))
             {
                 Cache = status;
                 this.Deserialize(Cache?.cacheBits);
             }
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine("Exception in RedisTokenCache.BeforeAccessNotification: " + ex.Message);
     }
 }
Ejemplo n.º 3
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)
     {
         Cache = new UserTokenCacheItem
         {
             cacheBits = this.Serialize(),
             LastWrite = DateTime.Now
         };
         try
         {
             var cache         = Redis.Connection.GetDatabase();
             var cacheItemJson = JsonConvert.SerializeObject(Cache);
             cache.StringSet(userId, cacheItemJson, TimeSpan.FromDays(1)); // could we use token expiry somehow?
         }
         catch (Exception ex)
         {
             Trace.WriteLine("Exception in RedisTokenCache.AfterAccessNotification: " + ex.Message);
         }
         this.HasStateChanged = false;
     }
 }