Пример #1
0
        private void Set(string key, string value, CookieOptions option, int?expireTime)
        {
            if (option == null)
            {
                option = new CookieOptions
                {
                    Expires = expireTime.HasValue
                        ? DateTime.Now.AddMinutes(expireTime.Value)
                        : DateTime.Now.AddDays(_cookieManagerOptions.DefaultExpireTimeInDays)
                };
            }

            if (_cookieManagerOptions.AllowEncryption)
            {
                var protecetedData = _dataProtector.Protect(value);
                var encodedValue   = Base64TextEncoder.Encode(protecetedData);
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, encodedValue, option);
            }
            else
            {
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, value, option);
            }
        }
Пример #2
0
        public string Get(string key)
        {
            if (_httpContext == null)
            {
                throw new ArgumentNullException(nameof(_httpContext));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (!Contains(key))
            {
                return(string.Empty);
            }
            var encodedValue = _chunkingHttpCookie.GetRequestCookie(_httpContext, key);

            if (!Base64TextEncoder.TryDecode(encodedValue, out var protectedData))
            {
                return(encodedValue);
            }
            return(_dataProtector.TryUnprotect(protectedData, out var unprotectedData) ? unprotectedData : encodedValue);
        }