asp.net,cookie,寫cookie,取cookie

來源:互聯網
上載者:User

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 = "/";

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.