码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET - cookie

时间:2015-09-30 23:16:50      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

 下面是写cookie 
 HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项 
 DateTime dt = DateTime.Now;//定义时间对象 
 TimeSpan ts=new TimeSpan(1,0,0,0);//cookie有效作用时间,具体查msdn 
 cookie.Expires = dt.Add(ts);//添加作用时间 
 cookie.Values.Add("user","cxbkkk");//增加属性 
 cookie.Values.Add("userid","1203"); 
 Response.AppendCookie(cookie);//确定写入cookie中 


 读取cookie  if(Request.Cookies["Info"]!=null)  {    string temp=Convert.ToString(Request.Cookies["Info"].Values["user"])+" "+Convert.ToString(Request.Cookies["Info"].Values["userid"]);    //读全部就用Request.Cookies["Info"].Value
}  if(temp=="")  {    Response.Write("");  }  else    Response.Write(temp);  }  else {   Response.Write("error"); }


修改cookie Response.Cookies[
"Info"]["user"] = "2"; Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1);


删除cookie下的属性 HttpCookie acookie
=Request.Cookies["Info"]; acookie.Values.Remove("userid"); acookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(acookie);


删除所有cookie,就是设置过期时间为现在就行了
int limit=Request.Cookies.Count - 1; for(int i=0;i<limit;i++) {   acookie = Request.Cookies(i)   acookie.Expires = DateTime.Now.AddDays(-1)   Response.Cookies.Add(acookie) }

 

ASP.NET - cookie

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4850431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!