#region cookie的操作
#region 获取cookie对象 public static HttpCookie GetCookieModel(string cookieName)
/// <summary>
/// 获取cookie对象
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookieModel(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
return cookie;
}
#endregion
#region 为cookie赋值 public static void SetCookie(string cookieName, String[] str,int count)
/// <summary>
/// 为cookie赋值
/// </summary>
/// <param name="cookieName">cookie的名字</param>
/// <param name="str">值所存放的数组</param>
/// <param name="count">cookie的有效时间(以天为单位)</param>
public static void SetCookie(string cookieName, String[] str, int count)
{
HttpCookie cookie = new HttpCookie(cookieName);
for (int i = 0; i < str.Length; i++)
{
cookie[i.ToString()] = str[i];
}
cookie.Expires = DateTime.Now.AddDays(count);
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion
#region 获取cookie的值 public static string[] GetCookie(string cookieName)
/// <summary>
/// 获取cookie的值
/// </summary>
/// <param name="cookieName">cookie的名字</param>
/// <returns>返回cookie值字符串数组</returns>
public static string[] GetCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
string[] str = null;
if (cookie != null)
{
str = new string[cookie.Values.Count];
for (int i = 0; i < str.Length; i++)
{
str[i] = cookie.Values[i].ToString();
}
return str;
}
else
{
str = new string[0];
return str;
}
}
#endregion
#region 清除cookie public static void ClearCookie(string cookieName)
/// <summary>
/// 清除cookie
/// </summary>
/// <param name="cookieName">要清除cookie的名字</param>
public static void ClearCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-2);
HttpContext.Current.Response.Cookies.Set(cookie);
}
}
#endregion
#endregion
#region 获取cookie对象 public static HttpCookie GetCookieModel(string cookieName)
/// <summary>
/// 获取cookie对象
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookieModel(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
return cookie;
}
#endregion
#region 为cookie赋值 public static void SetCookie(string cookieName, String[] str,int count)
/// <summary>
/// 为cookie赋值
/// </summary>
/// <param name="cookieName">cookie的名字</param>
/// <param name="str">值所存放的数组</param>
/// <param name="count">cookie的有效时间(以天为单位)</param>
public static void SetCookie(string cookieName, String[] str, int count)
{
HttpCookie cookie = new HttpCookie(cookieName);
for (int i = 0; i < str.Length; i++)
{
cookie[i.ToString()] = str[i];
}
cookie.Expires = DateTime.Now.AddDays(count);
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion
#region 获取cookie的值 public static string[] GetCookie(string cookieName)
/// <summary>
/// 获取cookie的值
/// </summary>
/// <param name="cookieName">cookie的名字</param>
/// <returns>返回cookie值字符串数组</returns>
public static string[] GetCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
string[] str = null;
if (cookie != null)
{
str = new string[cookie.Values.Count];
for (int i = 0; i < str.Length; i++)
{
str[i] = cookie.Values[i].ToString();
}
return str;
}
else
{
str = new string[0];
return str;
}
}
#endregion
#region 清除cookie public static void ClearCookie(string cookieName)
/// <summary>
/// 清除cookie
/// </summary>
/// <param name="cookieName">要清除cookie的名字</param>
public static void ClearCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-2);
HttpContext.Current.Response.Cookies.Set(cookie);
}
}
#endregion
#endregion