Sử dụng Cookie

Một phần của tài liệu Cấu trúc ASP NET framwork và cơ bản về c# (Trang 151 - 153)

Cookie làm việc như thế nào?

Khi trình duyệt web tạo một Cookie, một nội dung sẽ được lưu vào header của trang web với nội dung giống như sau:

Set-Cookie: Message=Hello

Phần tiêu đề Set-Cookie này gây ra cho trình duyệt web tạo một Cookie có tên là Message và giá trị của nó là Hello.

Sau khi một Cookie được tạo trên trình duyệt, Mỗi khi trình duyệt yêu cầu một trang web trong ứng dụng, trình duyệt sẽ gửi một header có dạng giống như sau:

Cookie: Message=Hello

Tiêu đề Cookie chứa đựng tất cả các Cookie mà được tạo trên Web Server. Cookie được gửi trở lại mỗi khi một yêu cầu được đưa ra trên trình duyệt web.

Bạn có thể tạo hai kiểu của Cookie, Session Cookies và Persistent Cookies. Session cookies chỉ tồn tại trong bộ nhớ khi trình duyệt web bị đóng lại thì nó cũng bị xóa đi.

Còn Persistent Cookies có thể tồn tại hàng tháng hoặc hàng năm. Khi bạn tạo một Persistent Cookies, nó sẽ được lưu trữ trên web browse trên máy tính của bạn. với IE ví dụ nó sẽ được lưu trữ trong một file Text theo thư mục

\Documents and Settings\[user]\Cookies

Còn với FireFox nó lưu trữ trong thư mục theo đường dẫn sau:

\Documents and Settings\[user]\Application Data\Mozilla\Firefox\Profiles\[random folder name]\Cookies.txt

bởi vì sự lưu trữ cookies trên các trình duyệt khác nhau để ở các thư mục khác nhau lên khi bạn tạo Cookies trên IE thì nó sẽ không tồn tại trên FireFox và ngược lại.

Bảo mật với Cookie Tạo Cookies

Bạn có thể tạo cookies với câu lệnh Response.Cookies, tất cả các Cookies sẽ được gửi từ Web Server đến Web Browser.

Ví dụ sau đây sẽ tạo ra một Cookies Message với giá trị được lấy từ hộp TextBox trên Form

Ví dụ 1: Trang setCookies.aspx

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="Default.aspx.cs" Inherits="_Default" %>

<script runat="server">

protected void Add_Click(object sender, EventArgs e) {

Response.Cookies["Message"].Value = txtCookies.Text; }

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

<title>Create Cookies</title> </head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Cookie Value"></asp:Label>

<asp:TextBox ID="txtCookies" runat="server"></asp:TextBox> (adsbygoogle = window.adsbygoogle || []).push({});

<asp:Button ID="Add" runat="server" OnClick"Add_Click" Text="Button" />

</div>

</form> </body> </html>

Trong ví dụ một là chúng ta tạo ra một Session Cookies, còn nếu bạn muốn tạo một Persistent Cookies bạn cần chỉ định thời hạn kết thúc cho Cookies .

Ví dụ 2 trang setPersistentCookies.aspx

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="setPersistentCookies.aspx.cs" Inherits="setPersistentCookies"

%>

<script runat="server">

protected void Page_Load(object sender, EventArgs e) {

int counter=0;

if (Request.Cookies["counter"] != null)

counter = Int32.Parse(Request.Cookies["counter"].Value); counter++;

Response.Cookies["counter"].Value = counter.ToString();

Response.Cookies["counter"].Expires = DateTime.Now.AddYears(2); this.Label1.Text = Response.Cookies["counter"].Value;

}

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Set Persitent Cookies</title> </head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</div>

</form> </body> </html>

Trong ví dụ trên khi chạy chương trình mỗi lần bạn Refresh lại trang thì giá trị của Label1 sẽ tăng lên một. Và với câu lệnh

Response.Cookies[“counter”].Expires=Datetime.Now.AddYears(2), có nghĩa là thời gian tồn tại của Cookie này sẽ là 2 năm.

Đọc dữ liệu từ Cookies

Bạn sử dụng lện Request.Cookies để lấy dữ liệu từ Cookies, bạn xem lại ví dụ 2 trang setPersistentCookies.aspx. (adsbygoogle = window.adsbygoogle || []).push({});

Khi bạn có một tập hợp các Cookies bạn có thể lấy tât cả giá trị của các Cookies trên website của mình, ví dụ sau đây sẽ hướng dẫn bạn làm việc đó.

Ví dụ 3 trang GetallCookies

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="GetAllCookies.aspx.cs" Inherits="GetAllCookies" %>

<script runat="server">

void Page_Load() {

ArrayList colCookies = new ArrayList();

for (int i = 0; i < Request.Cookies.Count; i++) colCookies.Add(Request.Cookies[i]);

grdCookies.DataSource = colCookies; grdCookies.DataBind();

}

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Get All Cookies</title> </head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="grdCookies" runat="server">

</asp:GridView>

</div>

</form> </body> </html>

Thiết lập thuộc tính cho Cookies

Cookies được đưa ra với lớp HttpCookie, khi bạn tạo hoặc lấy giá trị từ một Cookie có thể bạn sẽ sử dụng một vài thuộc tính của lớp này:

Một phần của tài liệu Cấu trúc ASP NET framwork và cơ bản về c# (Trang 151 - 153)