Esempio n. 1
0
        void ICacheSecurity.ValidateSet(string key, string value, CacheServiceOptions options)
        {
            var principal = GetAuthPrincipal();

            //Add additional checks here
            return;
        }
Esempio n. 2
0
        public void Set(string key, string value, CacheServiceOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using (log.Activity(m => m($"Setting Cache[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateSet(key, value, options);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                context.SetString(key, value, new DistributedCacheEntryOptions
                {
                    AbsoluteExpiration = options?.AbsoluteExpiration,
                    AbsoluteExpirationRelativeToNow = options?.AbsoluteExpirationRelativeToNow,
                    SlidingExpiration = options?.SlidingExpiration,
                });

                log.Info(m => m($"Setted Cache[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Esempio n. 3
0
        public static void SetObject <T>(this ICacheService obj, string key, T value, CacheServiceOptions options = default(CacheServiceOptions)) where T : class
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (options == null)
            {
                var def = new DistributedCacheEntryOptions();
                options = new CacheServiceOptions
                {
                    AbsoluteExpiration = def.AbsoluteExpiration,
                    AbsoluteExpirationRelativeToNow = def.AbsoluteExpirationRelativeToNow,
                    SlidingExpiration = def.SlidingExpiration,
                };
            }
            var pattern = JsonConvert.SerializeObject(value);

            obj.Set(key, pattern, options);
        }