时间:2021-07-01 10:21:17 帮助过:38人阅读
protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("user"); cookie.Value = "这是cookie"; cookie["sex"] = "女"; cookie.Values.Add("age", "18"); cookie.Expires = DateTime.Now.AddHours(1); Response.AppendCookie(cookie);//将Cookie追加到内部cookie //Response.Cookies.Add(cookie);//跟上面一样 }
2获取Cookie
protected void Button1_Click(object sender, EventArgs e) { //获取客户端发送的Cookie HttpCookie cookie1 = Request.Cookies["user"]; if (cookie1 == null) { Response.Write("木有发现cookie"); } else { Response.Write("cooki值为:"+cookie1.Value+"
"); Response.Write("sex值为:"+cookie1["sex"]+"
"); Response.Write("age值为:"+cookie1["age"]+"
"); } }
3,删除cookie
//将cookie有效期设置为过去某个时间,浏览器会检查自动删除 HttpCookie cookie = Request.Cookies["user"]; cookie.Expires = DateTime.Now.AddHours(-1);
cookie.Domain="baiyun.com";cookie.Domain="sell.baiyun.com";
这样cookie就能用于主域和子域了