Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一。Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一起发送到服务器。浏览器对 Cookie 的大小有限制,因此,只有不超过 4096 字节才能保证被接受。 编写Cookie //方式1: //方式2: //多值Cookie的写法 //方式1: //方式2:
If (Request.Cookies["userName"]!=null) //多值Cookie的读取
} 运行此代码时,可看到一个名为“ASP.NET_SessionId”的Cookie,ASP.NET用这个 Cookie 来保存您的会话的唯一标识符。 修改 Cookie 删除 Cookie
HttpCookie cookie = new HttpCookie("userinfo1");
------------- 如果有主站及二级域名站且cookie要共享的话则要加入如下设置 cookie.Domain = ".主域名";
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/huwei2003/archive/2009/01/17/3810068.aspx
<%@ Page Language="C#" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { // Create a new HttpCookie. HttpCookie myHttpCookie = new HttpCookie("LastVisit", DateTime.Now.ToString()); // By default, the HttpOnly property is set to false // unless specified otherwise in configuration. myHttpCookie.Name = "MyHttpCookie"; Response.AppendCookie(myHttpCookie); // Show the name of the cookie. Response.Write(myHttpCookie.Name); // Create an HttpOnly cookie. HttpCookie myHttpOnlyCookie = new HttpCookie("LastVisit", DateTime.Now.ToString()); // Setting the HttpOnly value to true, makes // this cookie accessible only to ASP.NET. myHttpOnlyCookie.HttpOnly = true; myHttpOnlyCookie.Name = "MyHttpOnlyCookie"; Response.AppendCookie(myHttpOnlyCookie); // Show the name of the HttpOnly cookie. Response.Write(myHttpOnlyCookie.Name); } </script> <html> <body> <script> function getCookie(NameOfCookie) { if (document.cookie.length > 0) { begin = document.cookie.indexOf(NameOfCookie+"="); if (begin != -1) { begin += NameOfCookie.length+1; end = document.cookie.indexOf(";", begin); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(begin, end)); } } return null; } </script> <script> // This code returns the cookie name. alert("Getting HTTP Cookie"); alert(getCookie("MyHttpCookie")); // Because the cookie is set to HttpOnly, // this returns null. alert("Getting HTTP Only Cookie"); alert(getCookie("MyHttpOnlyCookie")); </script> </body> </html> |
asp.net,cookie,写cookie,取cookie
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.