Esempio n. 1
0
        /// <summary>
        /// 设置Cookie的值
        /// </summary>
        /// <param name="cookieName">Cookie对象名称</param>
        /// <param name="value">Cookie值</param>
        /// <returns>执行结果</returns>
        public static bool SetCookieValue(string cookieName, string value, DateTime?expiresTime = null)
        {
            if (string.IsNullOrWhiteSpace(cookieName))
            {
                return(false);
            }
            var nowCookie = GetCookie(cookieName);

            if (nowCookie == null)
            {
                nowCookie = new CookieItem()
                {
                    Key = cookieName
                };
            }
            if (!expiresTime.HasValue)
            {
                expiresTime = DateTime.Now.AddHours(2);
            }
            var options = nowCookie.Options ?? new CookieOptions();

            nowCookie.Value   = value;
            options.Expires   = expiresTime.Value;
            nowCookie.Options = options;
            SaveCookie(nowCookie);
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 保存Cookie
        /// </summary>
        /// <param name="cookie">cookie对象</param>
        public static void SaveCookie(CookieItem cookie)
        {
            if (cookie == null)
            {
                return;
            }
            cookie.Options          = cookie.Options ?? new CookieOptions();
            cookie.Options.HttpOnly = true;

            HttpContextHelper.Current.Response.Cookies.Append(cookie.Key, cookie.Value, cookie.Options);
        }