C#操作Cookie

一、存储Cookie

HttpContext.Response.Cookies.Add(new HttpCookie(Cookie名称, Cookie内容));

二、读取Cookie

HttpContext.Request.Cookies[“Cookie名称”];

三、删除Cookie

通过新建一个立即过期的同内容cookie,将原有cookie进行覆盖,从而完成删除

HttpCookie newcookie;
newcookie = Request.Cookies["oldcookie"];
TimeSpan ts = new TimeSpan(0, 0, 0, 0);//时间跨度为0 
newcookie.Expires = DateTime.Now.Add(ts);//立即过期 
Response.Cookies.Add(newcookie);//写入立即过期的cookie

 

 
posted @ 2017-12-22 15:37  付刚的博客  阅读(4893)  评论(0编辑  收藏