Esempio n. 1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            // 1.
            var json = _cookieBuilder.Contextulize(context.HttpContext).Get(_cookieName);

            if (String.IsNullOrEmpty(json))
            {
                return;
            }

            // Deserialize alerts store
            var alerts = DeserializeAlerts(json);

            if (alerts == null)
            {
                _deleteCookie = true;
                return;
            }

            if (alerts.Count == 0)
            {
                return;
            }

            // Ensure alerts are available for the entire request
            _alerts = alerts;
        }
Esempio n. 2
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            // Get tracking cookie
            var value = _cookieBuilder.Contextulize(context.HttpContext).Get(_cookieName);

            // Cookie does not exist
            if (String.IsNullOrEmpty(value))
            {
                _active = false;
                return;
            }

            // We have an active cookie, ensure this is known for the entire request
            _active = true;
        }
Esempio n. 3
0
        public async Task <TEntity> IncrementAsync(TEntity entity)
        {
            // Transform tracking cookie into int array
            List <int> values = null;

            var storage = "";

            if (_context != null)
            {
                storage = _context.Request.Cookies[CookieName];
            }

            // Read existing into values
            if (!String.IsNullOrEmpty(storage))
            {
                values = storage.ToIntArray().ToList();
            }

            // Does the entity Id we are accessing exist in our store
            if (values != null)
            {
                if (values.Contains(entity.Id))
                {
                    return(entity);
                }
            }

            await UpdateTotalViewsAsync(entity);


            if (values == null)
            {
                values = new List <int>();
            }

            values.Add(entity.Id);

            // If a context is supplied use a client side cookie to track views
            // Expire the cookie every 10 minutes using a sliding expiration to
            // ensure views are updated often but not on every refresh
            _cookieBuilder
            .Contextulize(_context)
            .Append(CookieName, values.ToArray().ToDelimitedString(),
                    new CookieOptions
            {
                HttpOnly = true,
                Expires  = DateTime.Now.AddMinutes(10)
            });

            return(entity);
        }