ASP.net中的Cookies操作
有关Cookie的参考文献:http://rfc.net/rfc2109.html
以下为.net framework 2.0中Cookie的常用操作:
一、添加Cookie
二、读Cookie
三、写Cookie
四、清除Cookie
以下为.net framework 2.0中Cookie的常用操作:
一、添加Cookie
HttpCookie myCookie = new HttpCookie("CookieName");
myCookie.Values.Add("CookieItem1","CookieItem1Value");
myCookie.Values.Add("CookieItem2","CookieItem2Value");
myCookie.Expires = DateTime.Now.AddDays(30);
Response.AppendCookie(myCookie);
myCookie.Values.Add("CookieItem1","CookieItem1Value");
myCookie.Values.Add("CookieItem2","CookieItem2Value");
myCookie.Expires = DateTime.Now.AddDays(30);
Response.AppendCookie(myCookie);
二、读Cookie
HttpCookie myCookie = Request.Cookies["CookieName"];
if (myCookie != null)
{
string a = myCookie.Values["CookieItem1"].ToString();
string b = myCookie.Values["CookieItem2"].ToString();
}
if (myCookie != null)
{
string a = myCookie.Values["CookieItem1"].ToString();
string b = myCookie.Values["CookieItem2"].ToString();
}
三、写Cookie
HttpCookie myCookie = Request.Cookies["CookieName"];
if (myCookie != null)
{
if (myCookie.Values["Item1"] != null)
{
Response.Cookies["CookieName"]["Item1"] = "";
}
}
if (myCookie != null)
{
if (myCookie.Values["Item1"] != null)
{
Response.Cookies["CookieName"]["Item1"] = "";
}
}
四、清除Cookie
HttpCookie myCookie = HttpContext.Current.Response.Cookies["CookieObjectName"];
if(myCookie != null)
myCookie.Expires = DateTime.Now;
if(myCookie != null)
myCookie.Expires = DateTime.Now;