Esempio n. 1
0
        public static void Add(string key, object value, int?timeout)
        {
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookiesName];

            if (httpCookie == null)
            {
                httpCookie = new HttpCookie(CookiesName);
            }
            httpCookie.Expires     = DateTime.Now.AddYears(50);
            httpCookie.Domain      = CookiesDomain;
            httpCookie.Values[key] = HttpUtility.UrlEncode(value.ToString());
            httpCookie.Values[key + ExpireTimeStr] = ((!timeout.HasValue) ? HttpUtility.UrlEncode(CookiesExpire.ToString("yyyy-MM-dd HH:mm:ss")) : HttpUtility.UrlEncode(DateTime.Now.AddMinutes((double)timeout.Value).ToString("yyyy-MM-dd HH:mm:ss")));
            httpCookie.Values[ValidateName]        = GetValidateStr(httpCookie);
            HttpContext.Current.Response.Cookies.Add(httpCookie);
        }
Esempio n. 2
0
        /// <summary>
        /// 设置用户Cookie
        /// </summary>
        /// <param name="dic">键值集合</param>
        /// <param name="timeout">过期时间(分钟)</param>
        public static void Add(Dictionary <string, object> dic, int?timeout)
        {
            HttpCookie ck = HttpContext.Current.Request.Cookies[CookiesName];

            if (ck == null)
            {
                ck = new HttpCookie(CookiesName);
            }
            ck.Expires = DateTime.Now.AddYears(50);
            ck.Domain  = CookiesDomain;
            foreach (KeyValuePair <string, object> pair in dic)
            {
                ck.Values[pair.Key] = HttpUtility.UrlEncode(pair.Value.ToString( ));
                ck.Values[pair.Key + ExpireTimeStr] = !timeout.HasValue ? HttpUtility.UrlEncode(CookiesExpire.ToString("yyyy-MM-dd HH:mm:ss")) : HttpUtility.UrlEncode(DateTime.Now.AddMinutes(( double )timeout.Value).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            ck.Values[ValidateName] = GetValidateStr(ck);
            HttpContext.Current.Response.Cookies.Add(ck);
        }
Esempio n. 3
0
        public void Add(Dictionary <string, object> dic, int?timeout)
        {
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookiesName];

            if (httpCookie == null)
            {
                httpCookie = new HttpCookie(CookiesName);
            }
            httpCookie.Expires = DateTime.Now.AddYears(50);
            httpCookie.Domain  = _cookiespath;
            foreach (KeyValuePair <string, object> item in dic)
            {
                httpCookie.Values[item.Key] = HttpUtility.UrlEncode(item.Value.ToString());
                httpCookie.Values[item.Key + ExpireTimeStr] = ((!timeout.HasValue) ? HttpUtility.UrlEncode(CookiesExpire.ToString("yyyy-MM-dd HH:mm:ss")) : HttpUtility.UrlEncode(DateTime.Now.AddMinutes((double)timeout.Value).ToString("yyyy-MM-dd HH:mm:ss")));
            }
            httpCookie.Values[ValidateName] = GetValidateStr(httpCookie);
            HttpContext.Current.Response.Cookies.Add(httpCookie);
        }