cookie的读入和读出

写入cookie中

在mvc的控制器中

HttpCookie GetUserID = new HttpCookie("uID", 要保存的值); 

 GetUserID.Expires = DateTime.Now.AddDays(7);//设置过期时间

 this.Response.Cookies.Add(GetUserID);//  

也在mvc中在cookie中加入中文  得编码进去 再解码出来

HttpCookie GetUserName = new HttpCookie("uName", HttpUtility.UrlEncode(要存储的中文));//中文

 GetUserName.Expires = DateTime.Now.AddDays(7);//设置过期时间

this.Response.Cookies.Add(GetUserName);

读取cookie

GetName = HttpUtility.UrlDecode(this.Request.Cookies["uName"].Value//解码出来

GetUserID = this.Request.Cookies["uID"]; 

清除cookie

HttpCookie cookieName = new HttpCookie("uName");
cookieName.Values.Clear();
cookieName.Expires = DateTime.Now.AddDays(-1);
this.Response.Cookies.Add(cookieName);

(清除cookie还有别的方法 可以贴在下面  共同学习)

 

posted @ 2015-12-18 16:08  HEZG  阅读(193)  评论(0编辑  收藏  举报