写:
//设置Cookies
System.Web.HttpCookie newcookie = new HttpCookie(“master”);
newcookie.Values[“name”] = Server.UrlEncode(txtName.Text); //对写出的cookie值进行编码
if (this.RadioButton1.Checked)//这是记录是否记住登录的
newcookie.Expires = DateTime.Now.AddDays(365);
else
newcookie.Expires = DateTime.Now.AddDays(0);
Response.AppendCookie(newcookie);
读:
string MasterName = Server.UrlDecode(Request.Cookies[“master”][“name”]);
string MasterCookies = Server.UrlDecode(Request.Cookies[“master”][“cookies”]);//对写出的cookie值进行解码
—————————————————–
//创建一个Cookie对象
HttpCookie hc2=new HttpCookie(“user”);
hc2[“name”]=”max”
hc2[“Rank”]=3;
hc2.Expires=DateTime.Now.AddHours(1);
Response.Cookies.add(hc2);
//读取值
if(Request.Cookies[“user”] != null)
{
Response.write(“<li>username=”+Request.Cookies[“user”][“name”]);
Response.write(“<li>userRank=”+Request.Cookies[“user”][“Rank”]);
}
//以下为遍历Cookies
for(int i=0;i<Request.Cookies.Count;i++)
{
if(Request.Cookies[i].HasKeys)
{
for(int j=0;j<Request.Cookies[i].Values.Count;j++)
{
Response.Write(“<li>”+Request.Cookies[i].Name+”.”+Request.Cookies[i].Values.AllKeys[j]+”=”+Request.Cookise[i].values[j])
}
}else
{
Response.write(“<li>”+Request.Cookies[i].Name+”=”+Request.Cookies[i].value);
}
}
设置过期时间
switch(expires.Trim())
{
case “一小时”:
oCookie.Expires = DateTime.Now.AddHours(1);break;
case “一天”:
oCookie.Expires = DateTime.Now.AddDays(1);break;
case “一个月”:
oCookie.Expires = DateTime.Now.AddMonths(1);break;
case “一年”:
oCookie.Expires = DateTime.Now.AddYears(1);break;
case “永久”:
oCookie.Expires = DateTime.MaxValue; break;
default:
break;
}