using System;
using System.Web;
/// <summary>
/// Cookie 的摘要說明
/// </summary>
public class Cookie
{
/// <summary>
/// 建立Cookies
/// </summary>
/// <param name="strName">Cookie 主鍵</param>
/// <param name="strValue">Cookie 索引值</param>
/// <param name="strDay">Cookie 天數</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.setCookie("主鍵","索引值","天數");</code>
public bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 讀取Cookies
/// </summary>
/// <param name="strName">Cookie 主鍵</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.getCookie("主鍵");</code>
public string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return Cookie.Value.ToString();
}
else
{
return null;
}
}
/// <summary>
/// 刪除Cookies
/// </summary>
/// <param name="strName">Cookie 主鍵</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.delCookie("主鍵");</code>
public bool delCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
}