c#.net與vb.net中讀寫Cookie的方法!

來源:互聯網
上載者:User

Cookie (HttpCookie的執行個體)提供了一種在 Web 應用程式中儲存使用者特定資訊的方法。例如,當使用者訪問您的網站時,您可以使用 Cookie 儲存使用者喜好設定或其他資訊。當該使用者再次訪問您的網站時,應用程式便可以檢索以前儲存的資訊。

C#.net部分

建立Cookie方法 (1)
Response.Cookies["userName"].Value = “admin";
Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);
//如果不設定失效時間,Cookie資訊不會寫到使用者硬碟,瀏覽器關閉將會丟棄。

建立Cookie方法 (2)
HttpCookie aCookie = new HttpCookie(“lastVisit”); //上一次訪問時間
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

訪問Cookie方法(1)
if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);訪問Cookie方法(2)
if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

建立多值Cookie方法 (1)
Response.Cookies["userInfo"]["userName"] = “admin";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

建立多值Cookie方法 (2)
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = “admin";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

讀取多值Cookie

HttpCookie aCookie = Request.Cookies["userInfo"];
string userName=aCookie.Values[“userName”];
string lastVisit=aCookie.Values[“lastVisit”];

vb.net部分

建立Cookie方法 (1)
Response.Cookies("userName").Value = “admin"
Response.Cookies(“userName”).Expires = DateTime.Now.AddDays(1)
'如果不設定失效時間,Cookie資訊不會寫到使用者硬碟,瀏覽器關閉將會丟棄。
 
 
建立Cookie方法 (2)
Dim aCookie As HttpCookie =  New HttpCookie(“lastVisit”)  '上一次訪問時間
aCookie.Value = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
 
 
訪問Cookie方法(1)
If Not Request.Cookies("userName") Is Nothing Then
Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)訪問Cookie方法(2)
End If
If Not Request.Cookies("userName") Is Nothing Then
Dim aCookie As HttpCookie =  Request.Cookies("userName")
Label1.Text = Server.HtmlEncode(aCookie.Value)
End If
 
 
建立多值Cookie方法 (1)
Response.Cookies("userInfo")("userName") = “admin"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
 
 
建立多值Cookie方法 (2)
Dim aCookie As HttpCookie =  New HttpCookie("userInfo")
aCookie.Values("userName") = “admin"
aCookie.Values("lastVisit") = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
 
 
讀取多值Cookie
 
 
Dim aCookie As HttpCookie =  Request.Cookies("userInfo")
Dim userName As String = aCookie.Values(“userName”)
Dim lastVisit As String = aCookie.Values(“lastVisit”)
 
 
修改和刪除Cookie
 
不能直接修改或刪除Cookie,只能建立一個新的Cookie,發送到用戶端以實現修改或刪除Cookie.

相關文章

聯繫我們

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