asp.net,cookie,寫cookie,取cookie

Cookie是一段文本信息,在客戶端存儲 Cookie 是 ASP.NET 的會話狀態將請求與會話關聯的方法之一。Cookie 也可以直接用於在請求之間保持數據,但數據隨後將存儲在客戶端並隨每個請求一起發送到服務器。瀏覽器對 Cookie 的大小有限制,因此,只有不超過 4096 字節才能保證被接受。

編寫Cookie

//方式1:
Response.Cookies["username"].value="mike";
Response.Cookies["username"].Expires=DateTime.MaxValue;

//方式2:
HttpCookie acookie = new HttpCookie("last");
acookie.Value="a";
acookie..Expires=DateTime.MaxValue;
Response.Cookies.Add(acookie);

//多值Cookie的寫法

//方式1:
Response.Cookies["userinfo1"]["name"].value="mike";
Response.Cookies["userinfo1"]["last"].value="a";
Response.Cookies["userinfo1"].Expires=DateTime.MaxValue;

//方式2:
HttpCookie cookie = new HttpCookie("userinfo1");
cookie.Values["name"]="mike";
cookie.Values["last"]="a";
cookie.Expires=DateTime.MaxValue;
//cookie.Expires = System.DateTime.Now.AddDays(1);//設置過期時間 1天
Response.Cookies.Add(cookie);

 


讀取Cookie
Internet Explorer 將站點的 Cookie 保存在文件名格式爲 <user>@<domain>.txt 的文件中,其中 <user> 是您的帳戶名。
注意:在獲取Cookie的值之前,應該確保該 Cookie 確實存在。否則,您將得到一個異常

 

If (Request.Cookies["userName"]!=null)
{
string str = Request.Cookies("userName").Value;
}

//多值Cookie的讀取
If ( Request.Cookies["userInfo1"]!=null )
{
string name=Request.Cookies["userInfo1"]["name"];
string last=Request.Cookies["userInfo1"]["last"];
}


//讀取 Cookie 集合
for(int i = 0 ;i<Request.Cookies.Count ;i++)
{
    HttpCookie cookies = Request.Cookies;
    Response.Write("name="+cookies.Mame+"<br/>");
    if (cookies.HasKeys )//是否有子鍵
    {
        System.Collections.Specialized.NameValueCollection NameColl
                                             = aCookie.Values ;
        for(int j=0;j<NameColl.Count;j++)
        {
            Response.Write("子鍵名="+ NameColl.AllKey[j] +"<br/>");
            Response.Write("子鍵值="+ NameColl[j] +"<br/>");
        }

    }
    else
    {
        Response.Write("value="+cookies.Value+"<br/>");       
    }
}

運行此代碼時,可看到一個名爲“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);


修改cookie
1 Response.Cookies["Info"]["user"] = "2";
2 Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1);        刪除cookie下的屬性
1 HttpCookie acookie=Request.Cookies["Info"];
2 acookie.Values.Remove("userid");
3 acookie.Expires = DateTime.Now.AddDays(1);
4 Response.Cookies.Add(acookie);        刪除所有cookie,就是設置過期時間爲現在就行了
1 int limit=Request.Cookies.Count - 1;
2 for(int i=0;i<limit;i++)
3 {
4     acookie = Request.Cookies(i)
5     acookie.Expires = DateTime.Now.AddDays(-1)
6     Response.Cookies.Add(acookie)
7 }   

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

如果有主站及二級域名站且cookie要共享的話則要加入如下設置

cookie.Domain = ".主域名";
cookie.Path = "/";

 

本文來自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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章