62,046
社区成员
发帖
与我相关
我的任务
分享
/// <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="strCookieName">COOKIE对象名</param>
/// <param name="iExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param>
/// <param name="KeyValue">键/值对集合</param>
public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue)
{
HttpCookie objCookie = new HttpCookie(strCookieName.Trim());
foreach (String key in KeyValue.AllKeys)
{
objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim());
}
if (iExpires > 0)
{
if (iExpires == 1)
{
objCookie.Expires = DateTime.MaxValue;
}
else
{
objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
}
}
HttpContext.Current.Response.Cookies.Add(objCookie);
}