/// <summary>
///CookiesHelper 的摘要说明
/// </summary>
public class CookiesHelper
{
public CookiesHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region 设定cookie
/// <summary>
/// 设定cookie
/// </summary>
/// <param name="cookiename"></param>
/// <param name="cookievalue"></param>
public static void SetCookie(string cookiename, string cookievalue)
{
SetCookie(cookiename, cookievalue, DateTime.MinValue);
}
public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
{
if (cookiename != null && cookievalue != null)
{
HttpCookie cookie = new HttpCookie(cookiename);
cookie.Value = (cookievalue.Length == 0) ? String.Empty : System.Web.HttpUtility.UrlEncode(cookievalue);
if (expires > DateTime.MinValue)
cookie.Expires = expires;
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
#endregion
#region 清除指定cookie
/// <summary>
/// 清除指定cookie
/// </summary>
/// <param name="cookiename"></param>
public static void ClearCookie(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddYears(-3);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
#endregion
#region 得到指定的Cookie值
/// <summary>
/// 得到指定的Cookie值
/// </summary>
/// <param name="cookiename"></param>
public static string GetCookieValue(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
string cookievalue = String.Empty;
if (cookie != null)
{
cookievalue = cookie.Value;
}
if (cookievalue.Length > 0)
cookievalue = HttpUtility.UrlDecode(cookievalue);
return cookievalue;
}
#endregion
static HttpCookie hc;
static HttpCookieCollection hccol;
/// <summary>
/// 设置cookie
/// </summary>
/// <param name="name">名称</param>
/// <param name="value">值</param>
/// <param name="expire">过期时间,以分钟为单位</param>
public static void setCookie(string name, string value, double expire)
{
hc = new HttpCookie(name);
hc.Value = value;
hc.Expires = DateTime.Now.AddMinutes(expire);
HttpContext.Current.Response.AppendCookie(hc);
}
/// <summary>
/// 设置cookie
/// </summary>
/// <param name="name">名称</param>
/// <param name="value">值</param>
/// <param name="expire">过期时间,以天为单位</param>
public static void setCookie(string name, string value, int days)
{
hc = new HttpCookie(name);
hc.Value = value;
hc.Expires = DateTime.Now.AddDays(days);
HttpContext.Current.Response.AppendCookie(hc);
}
/// <summary>
/// 获取cookie
/// </summary>
/// <param name="name">名称</param>
/// <returns>值</returns>
public static string getCookie(string name)
{
hc = HttpContext.Current.Request.Cookies[name];
return hc == null ? null : hc.Value;
}
/// <summary>
/// 清除cookie
/// </summary>
/// <param name="name">名称</param>
public static void clearCookie(string name)
{
hc = new HttpCookie(name);
hc.Expires = DateTime.Today.AddDays(-1);
HttpContext.Current.Response.SetCookie(hc);
}
/// <summary>
/// 清空cookie
/// </summary>
public static void clearAllCookies()
{
hccol = new HttpCookieCollection();
foreach (HttpCookie hc0 in hccol)
{
hc0.Expires = DateTime.Today.AddDays(-1);
HttpContext.Current.Response.SetCookie(hc);
}
}
}