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); }
/// <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); }
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); }