增加: 第一種方法:Response.Cookies[“UserName”].Value=”張三” Response.Cookies[“UserName”].Expires=DateTime.Now.AddDays(1); 第二種方法: HttpCookie cookie=new HttpCooki ...
增加:
第一種方法:Response.Cookies[“UserName”].Value=”張三”
Response.Cookies[“UserName”].Expires=DateTime.Now.AddDays(1);
第二種方法:
HttpCookie cookie=new HttpCookie(“UserName”);
cookie.Value=”張三”;
cookie.Expires=DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
多值Cookie(子鍵):
第一種方法:
Response.Cookies[“UserInfo”][“userName”]=”張三”;
Response.Cookies[“UserInfo”][“pwd”]=”123”;
Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays(1);
第二種方法:
HttpCookie cookie=new HttpCOokie(“UserInfo”);
cookie.Values(“userName”)=”張三”;
cookie.Values(“pwd”)=”123”;
cookie.Expires=DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
刪除:
刪除單個Cookie
Response.Cookies[“userName”].Expires=DateTime.Now.AddDays(-1);
刪除所有的Cookie
HttpCookie aCookie;
String cookieName;
int count=Request.Cookies.Count;
for(int i=0;i<count;i++)
{
cookieName=Request.Cookies[i].Name;
aCookie=new HttpCookie(cookieName);
aCookie.Expires=DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
刪除單個子鍵
HttpCookie aCookie=Request.Cookies[“userInfo”];
aCookie.Values.Remove(“userName”);
aCookie.Expires=DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
修改:
修改單個Cookie
Response.Cookies[“userName”].Value=”李四”;
Response.Cookies[“userName”].Expires=DateTime.Now.AddDays(1);
修改單個子鍵
Response.Cookies[“UserInfo”][“pwd”]=”234”;
Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays(1);
讀取:
If(Request.Cookies[“userName”]!=null)
{
Lable1.Text=Request.Cookies[“userName”].Value;
}
或者
If(Request.Cookies[“userName”]!=null)
{
HttpCookie cookie=Request.Cookies[“userName”];
Lable.Text=cookie.Value;
}
多值讀取:
If(Request.Cookies[“UserInfo”]!=null)
{
Lable1.Text=Request.Cookies[“UserInfo”][“userName”];
Lable2.Text=Request.Cookies[“UserInfo”][“pwd”];
}
讀取所有的Cookie
for(int i=0;i<Request.Cookies.Count;i++)
{
aCookie=Request.Cookies[i];
string name=aCookie.Name;
string value=aCookie.Value;
}