asp.net cookie操作

來源:互聯網
上載者:User

asp教程.net cookie操作
cookie 限制到某個檔案夾或應用程式

httpcookie appcookie = new httpcookie("appcookie");
appcookie.value = "written " + datetime.now.tostring();
appcookie.expires = datetime.now.adddays(1);
appcookie.path = "/application1";
response.cookies.add(appcookie);

cookie清除

httpcookie cookie = request.cookies["username"];
cookie.expires = datetime.now.adddays(-30);
response.cookies.add(cookie);


cookie操作

response.cookies["userinfo"]["username"] = "patrick";
response.cookies["userinfo"]["lastvisit"] = datetime.now.tostring();
response.cookies["userinfo"].expires = datetime.now.adddays(1);

httpcookie acookie = new httpcookie("userinfo");
acookie.values["username"] = "patrick";
acookie.values["lastvisit"] = datetime.now.tostring();
acookie.expires = datetime.now.adddays(1);
response.cookies.add(acookie);

認情況下,一個網站的全部 cookie 都一起儲存在用戶端上,而且所有 cookie 都會隨著對該網站發送的任何請求一起發送到伺服器。也就是說,一個網站中的每個頁面都能獲得該網站的所有 cookie。但是,可以通過兩種方式設定 cookie 的範


限制 cookie 的域範圍

response.cookies["domain"].value = datetime.now.tostring();
response.cookies["domain"].expires = datetime.now.adddays(1);
response.cookies["domain"].domain = "support.contoso.com";


讀取 cookie
瀏覽器向伺服器發出請求時,會隨請求一起發送該伺服器的 cookie。在 asp.net教程 應用程式中,可以使用 httprequest 對象讀取 cookie,該對象可用作 page 類的 request 屬性使用。httprequest 對象的結構與 httpresponse 對象的結構基本相同,因此,可以從 httprequest 對象中讀取 cookie,並且讀取方式與將 cookie 寫入 httpresponse 對象的方式基本相同。下面的程式碼範例示範兩種方法,通過這兩種方法可擷取名為 username 的 cookie 的值,並將其值顯示在 label 控制項中:

if(request.cookies["username"] != null)
    label1.text = server.htmlencode(request.cookies["username"].value);

if(request.cookies["username"] != null)
{
    httpcookie acookie = request.cookies["username"];
    label1.text = server.htmlencode(acookie.value);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.