·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> asp.net各种cookie代码和解析

asp.net各种cookie代码和解析

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net各种cookie代码和解析

Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一。Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一起发送到服务器。浏览器对 Cookie 的大小有限制,因此,只有不超过 4096 字节才能保证被接受。编写Cookie

 1 //方式1: 2 Response.Cookies["username"].value="mike"; 3 Response.Cookies["username"].Expires=DateTime.MaxValue;  4  5 //方式2: 6 HttpCookie acookie = new HttpCookie("last"); 7 acookie.Value="a"; 8 acookie..Expires=DateTime.MaxValue;  9 Response.Cookies.Add(acookie);10 11 //多值Cookie的写法 http://www.cnblogs.com/sosoft/12 13 //方式1:14 Response.Cookies["userinfo1"]["name"].value="mike";15 Response.Cookies["userinfo1"]["last"].value="a";16 Response.Cookies["userinfo1"].Expires=DateTime.MaxValue; 17 18 //方式2:19 HttpCookie cookie = new HttpCookie("userinfo1");20 cookie.Values["name"]="mike";21 cookie.Values["last"]="a";22 cookie.Expires=DateTime.MaxValue; 23 //cookie.Expires = System.DateTime.Now.AddDays(1);//设置过期时间  1天24 Response.Cookies.Add(cookie);

读取CookieInternet Explorer 将站点的 Cookie 保存在文件名格式为 <user>@<domain>.txt 的文件中,其中 <user> 是您的帐户名。注意:在获取Cookie的值之前,应该确保该 Cookie 确实存在。否则,您将得到一个异常

 1 If (Request.Cookies["userName"]!=null) 2 { 3   string str = Request.Cookies("userName").Value;  4 } 5  6 //多值Cookie的读取 7 If ( Request.Cookies["userInfo1"]!=null ) 8 { 9   string name=Request.Cookies["userInfo1"]["name"];10   string last=Request.Cookies["userInfo1"]["last"]; 11 }12 13 14 //读取 Cookie 集合15 for(int i = 0 ;i<Request.Cookies.Count ;i++)16 {17     HttpCookie cookies = Request.Cookies;18     Response.Write("name="+cookies.Mame+"<br/>");19     if (cookies.HasKeys )//是否有子键20     {21         System.Collections.Specialized.NameValueCollection NameColl 22                                              = aCookie.Values ;23         for(int j=0;j<NameColl.Count;j++)24         {25             Response.Write("子键名="+ NameColl.AllKey[j] +"<br/>");26             Response.Write("子键值="+ NameColl[j] +"<br/>");27         }28 29     }30     else31     {32         Response.Write("value="+cookies.Value+"<br/>");        33     }34 }

运行此代码时,可看到一个名为“ASP.NET_sessionId”的Cookie,ASP.NET用这个 Cookie 来保存您的会话的唯一标识符。修改 Cookie修改的方法与创建方法相同删除 Cookie将其有效期设置为过去的某个日期。当浏览器检查 Cookie 的有效期时,就会删除这个已过期的 Cookie。HttpCookie cookie = new HttpCookie("userinfo1");cookie.Expires=DateTime.Now.AddDays(-30);Response.Cookies.Add(cookie);修改cookieResponse.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)}

-------------

如果有主站及二级域名站且cookie要共享的话则要加入如下设置

cookie.Domain = ".主域名";//例如.keleyi.comcookie.Path = "/";

Cookie.Expires AddDays(-1)是立即过期