1.添加
HttpCookie cookies = new HttpCookie("Porschev");//建立一个cookie
cookies["name"] = "Zhong Wei";//添加cookie
cookies["sex"] = "1";
cookies.Expires = DateTime.Now.AddDays(1);//需设置存活时间,这里设置成一天
Response.Cookies.Add(cookies);//表示返回信息时,顺带返回cookie
2.修改
HttpCookie cookies = Request.Cookies["Porschev"];
cookies["name"] = "wilson Z";
Response.Cookies["Porschev"].Expires = DateTime.Now.AddMinutes(-1); //修改好需加上时间,否则上一时间到时自动删除
3.删除
foreach (string cookiename in Request.Cookies.AllKeys)//查找如有cookie的值
{
HttpCookie cookies = Request.Cookies[cookiename];//找到相应值,如果只删除一个的话可指定名称
if (cookies != null)
{
cookies.Expires = DateTime.Today.AddDays(-1);//将cookie存活时间变成昨天
Response.Cookies.Add(cookies);//保存时通知客户端
Request.Cookies.Remove(cookiename);//移除cookie
}
}
HttpCookie cookies = new HttpCookie("Porschev");//建立一个cookie
cookies["name"] = "Zhong Wei";//添加cookie
cookies["sex"] = "1";
cookies.Expires = DateTime.Now.AddDays(1);//需设置存活时间,这里设置成一天
Response.Cookies.Add(cookies);//表示返回信息时,顺带返回cookie
2.修改
HttpCookie cookies = Request.Cookies["Porschev"];
cookies["name"] = "wilson Z";
Response.Cookies["Porschev"].Expires = DateTime.Now.AddMinutes(-1); //修改好需加上时间,否则上一时间到时自动删除
3.删除
foreach (string cookiename in Request.Cookies.AllKeys)//查找如有cookie的值
{
HttpCookie cookies = Request.Cookies[cookiename];//找到相应值,如果只删除一个的话可指定名称
if (cookies != null)
{
cookies.Expires = DateTime.Today.AddDays(-1);//将cookie存活时间变成昨天
Response.Cookies.Add(cookies);//保存时通知客户端
Request.Cookies.Remove(cookiename);//移除cookie
}
}