using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
namespace Jhgl.Smart
{
///
/// COOKIE操作类
/// public class COOKIE
{
///
/// 保存一个COOKIE
/// ///
COOKIE名称
///
COOKIE值
///
COOKIE过期时间(小时),0为关闭页面失效
public static void SaveCOOKIE(string COOKIEName, string COOKIEValue, double COOKIETime)
{
HttpCOOKIE myCOOKIE = new HttpCOOKIE(COOKIEName);
DateTime now = DateTime.Now;
myCOOKIE.Value = COOKIEValue;
if (COOKIETime != 0)
{
//有两种方法,第一方法设置COOKIE时间的话,关闭浏览器不会自动清除COOKIE
//第二方法不设置COOKIE时间的话,关闭浏览器会自动清除COOKIE ,但是有效期
//多久还未得到证实。
myCOOKIE.Expires = now.AddDays(COOKIETime);
if (HttpContext.Current.Response.COOKIEs[COOKIEName] != null)
HttpContext.Current.Response.COOKIEs.Remove(COOKIEName);
HttpContext.Current.Response.COOKIEs.Add(myCOOKIE);
}
else
{
if (HttpContext.Current.Response.COOKIEs[COOKIEName] != null)
HttpContext.Current.Response.COOKIEs.Remove(COOKIEName);
HttpContext.Current.Response.COOKIEs.Add(myCOOKIE);
}
}
///
/// 取得COOKIEValue
/// ///
COOKIE名称
///
COOKIE的值 public static string GetCOOKIE(string COOKIEName)
{
HttpCOOKIE myCOOKIE = new HttpCOOKIE(COOKIEName);
myCOOKIE = HttpContext.Current.Request.COOKIEs[COOKIEName];
if (myCOOKIE != null)
return myCOOKIE.Value;
else
return null;
}
///
/// 清除COOKIEValue
/// ///
COOKIE名称
public static void ClearCOOKIE(string COOKIEName)
{
HttpCOOKIE myCOOKIE = new HttpCOOKIE(COOKIEName);
DateTime now = DateTime.Now;
myCOOKIE.Expires = now.AddYears(-2);
HttpContext.Current.Response.COOKIEs.Add(myCOOKIE);
}
}
}