示例#1
0
        /// <summary>
        /// Get the key value
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>value</returns>
        public string Get(string key)
        {
            if (_httpContext == null)
            {
                throw new ArgumentNullException(nameof(_httpContext));
            }

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

            if (Contains(key))
            {
                var encodedValue  = _chunkingHttpCookie.GetRequestCookie(_httpContext, key);
                var protectedData = string.Empty;
                //allow encryption is optional
                //may change the allow encryption to avoid this first check if cookie value is able to decode than unprotect tha data
                if (Base64TextEncoder.TryDecode(encodedValue, out protectedData))
                {
                    string unprotectedData;
                    if (_dataProtector.TryUnprotect(protectedData, out unprotectedData))
                    {
                        return(unprotectedData);
                    }
                }
                return(encodedValue);
            }

            return(string.Empty);
        }
示例#2
0
        private void Set(string key, string value, CookieOptions option, int?expireTime)
        {
            if (option == null)
            {
                option = new CookieOptions();

                if (expireTime.HasValue)
                {
                    option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
                }
                else
                {
                    option.Expires = DateTime.Now.AddDays(_cookieManagerOptions.DefaultExpireTimeInDays);
                }
            }

            //check for encryption
            if (_cookieManagerOptions.AllowEncryption)
            {
                string protecetedData = _dataProtector.Protect(value);
                var    encodedValue   = Base64TextEncoder.Encode(protecetedData);
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, encodedValue, option);
            }
            else
            {
                //just append the cookie
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, value, option);
            }
        }