1.遍历所有cookie
foreach (string _key in Request.Cookies.AllKeys)//遍历主键
{
HttpCookie ck = Request.Cookies[_key];//得到主键
foreach (string sub_key in ck.Values.Keys)//遍历所有子键
{
TextBox3.Text += _key + "|" + sub_key + ":" + ck.Values[sub_key] + "
";
}
}
2.删除cookie
foreach (string _key in Request.Cookies.AllKeys)
{
Response.Cookies[_key].Expires = DateTime.MinValue;//设置所有主键过期
}
3.写入cookie
string s1 = TextBox1.Text.Trim(), s2 = TextBox2.Text.Trim();
HttpCookie ck_css = new HttpCookie("CSS");
ck_css.Values.Add("color", s1);
ck_css.Values.Add("fontsize", s2);
ck_css.Expires = DateTime.Now.AddDays(1);//设置过期时间
Response.Cookies.Add(ck_css);//写入
4.读取cookie
HttpCookie ck_css = Request.Cookies["CSS"];//读取主键为css的cookie值
string s1 = ck_css.Values["color"];//读取子键值
string s2 = ck_css.Values["fontsize"];
Label1.Style.Add("color", s1);//设置空间css样式
Label1.Style.Add("font-size", s2);