一、创建、读取具有多个键值对的cookie
1、创建
(1)HttpCookie hc=new HttpCookie("visitor");
hc.Values["username"]=this.TextBox1.Text;
hc.Values["userpassword"]=this.TextBox2.Text;
hc.Expires=DateTime.Now.AddDays(1);
(2)Response.Cookies.Add(hc);
Response.Cookies["visitor"]["username"]=this.TextBox1.Text;
Response.Cookies["visitor"]["password"]=this.TextBox2.Text;
Response.Cookies["visitor"].Expires=DateTime.Now.AddDays(1);
2、读取
(1)
HttpCookie hc=Request.Cookies["visitor"];
if(hc.haskeys)
{Response.Write("the username :"+hc.Values["username"].ToString());
Response.Write("the password :"+hc.Values["userpassword"].ToString());
}
(2)if(Request.Cookies["visitor"].HasKeys)
{
System.Collections.Specialized.NameValueCollection userinfo=new System.Collections.Specialized.NameValueCollection();
userinfo=Request.Cookies["visitor"].Values;
Response.Write("the username :"+userinfo["username"].ToString());
Response.Write("the password :"+userinfo["userpassword"].ToString());
}