Beispiel #1
0
        /// <summary>
        /// 根据键名获取Cookie值
        /// </summary>
        /// <param name="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;
                if (Base64TextEncoder.TryDecode(encodedValue, out protectedData))
                {
                    string unprotectedData;
                    if (_dataProtector.TryUnprotect(protectedData, out unprotectedData))
                    {
                        return(unprotectedData);
                    }
                }
                return(encodedValue);
            }

            return(string.Empty);
        }
Beispiel #2
0
        /// <summary>
        /// 设置Cookie信息
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">值</param>
        /// <param name="option">Cookie选项</param>
        /// <param name="expireTime">过期时间</param>
        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);
                }
            }

            //判断是否需要加密
            if (_cookieManagerOptions.AllowEncryption)
            {
                string protecetedData = _dataProtector.Protect(value);
                var    encodedValue   = Base64TextEncoder.Encode(protecetedData);
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, encodedValue, option);
            }
            else
            {
                _chunkingHttpCookie.AppendResponseCookie(_httpContext, key, value, option);
            }
        }