创建/删除Cookie数据

//1.编写(创建 和 修改 一样)
HttpCookie cookie = new HttpCookie("userName");
cookie.Value = "顾志海";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

//2.读取
if (Request.Cookies["userName"] != null)
{
string userName = Request.Cookies["userName"].Value;
userName = Server.HtmlEncode(userName);//确保恶意用户没有向 Cookie 中添加可执行脚本
}

//3.删除
HttpCookie cookie = new HttpCookie("userName");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);

posted @ 2018-06-23 01:05 顾德博 阅读(...) 评论(...) 编辑 收藏