/// <summary> /// 创建COOKIE对象并赋Value值 /// </summary> /// <param name="cookiesName">COOKIE对象名</param> /// <param name="IExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> /// <param name="cookiesValue">COOKIE对象Value值</param> public static void WriteCookies(string cookiesName, string cookiesValue, int IExpires = 1) { HttpCookie objCookie = new HttpCookie(cookiesName.Trim()); objCookie.Value = CoralSecurity.DesEncrypt(cookiesValue.Trim()); //加密存储 if (IExpires > 0) { if (IExpires == 1) { objCookie.Expires = DateTime.MaxValue; } else { objCookie.Expires = DateTime.Now.AddMinutes(IExpires); } } HttpContext.Current.Response.Cookies.Add(objCookie); }
/// <summary> /// 创建COOKIE对象并赋多个KEY键值 /// 设键/值如下: /// NameValueCollection myCol = new NameValueCollection(); /// myCol.Add("red", "rojo"); /// myCol.Add("green", "verde"); /// myCol.Add("blue", "azul"); /// myCol.Add("red", "rouge"); 结果“red:rojo,rouge;green:verde;blue:azul” /// </summary> /// <param name="cookiesName">COOKIE对象名</param> /// <param name="expires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param> /// <param name="cookiesKeyValueCollection">键/值对集合</param> public static void WriteCookies(string cookiesName, int expires, NameValueCollection cookiesKeyValueCollection) { HttpCookie objCookie = new HttpCookie(cookiesName.Trim()); foreach (String key in cookiesKeyValueCollection.AllKeys) { objCookie[key] = CoralSecurity.DesEncrypt(cookiesKeyValueCollection[key].Trim()); } if (expires > 0) { if (expires == 1) { objCookie.Expires = DateTime.MaxValue; } else { objCookie.Expires = DateTime.Now.AddSeconds(expires); } } HttpContext.Current.Response.Cookies.Add(objCookie); }