Friday 30 August 2013

ASP.NET Cookies

A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.
A Visual Studio project with source code is available to accompany this topic: Download.

Scenarios
Cookies provide a means in Web applications to store user-specific information. For example, when a user visits your site, you can use cookies to store user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier.

Cookie Limitations
Most browsers support cookies of up to 4096 bytes. Because of this small limit, cookies are best used to store small amounts of data, or better yet, an identifier such as a user ID. The user ID can then be used to identify the user and read user information from a database or other data store. (See the section "Cookies and Security" below for information about security implications of storing user information.)

How to: Write a Cookie
Cookies provide a means in Web applications to store user-specific information, such as history or user preferences. A cookie is a small bit of text that accompanies requests and responses as they go between the Web server and client. The cookie contains information that the Web application can read whenever the user visits the site.

The browser manages the cookies on client computers. Cookies are sent to the client using the HttpResponse object, which exposes a property called Cookies. Any cookies that you want your Web application to send to the browser must be added to this collection. When you write a new cookie, you must specify the Name and Value. Each cookie must have a unique name so that your Web application can identify it when the browser sends it with future requests.

There are two ways to write a cookie to a user's computer. You can either directly set cookie properties on the Cookiescollection or you can create an instance of the HttpCookie object and add it to the Cookies collection. You must create cookies before the ASP.NET page is rendered to the client. For example, you can write a cookie in a Page_Load event handler but not in a Page_Unload event handler. 

To write a cookie by setting cookie properties on the Cookies collection
In the ASP.NET page you want to write a cookie, assign properties to a cookie in the Cookies collection.


The following code example shows a cookie named UserSettings with the values of the subkeys Font and Color set. It also sets the expiration time to be tomorrow.
Response.Cookies["UserSettings"]["Font"] = "Arial";
Response.Cookies["UserSettings"]["Color"] = "Blue";
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);

To write a cookie by creating an instance of the HttpCookie object
  • Create an object of type HttpCookie and assign it a name.
  • Assign values to cookie's subkeys and set any cookie properties.
  • Add the cookie to the Cookies collection.
The following code example shows an instance of the HttpCookie object named myCookie, which represents a cookie named UserSettings.


HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);

How to: Read a Cookie
Cookies provide a means in Web applications to store user-specific information, such as
history or user preferences. A cookie is a small bit of text that accompanies requests and
responses as they go between the Web server and client. The cookie contains information
that the Web application can read whenever the user visits the site.

The browser is responsible for managing cookies on a user system. Cookies are sent to the
server with a page request and are accessible as part of the HttpRequest object, which
exposes a Cookies collection. You can read only cookies that have been created by pages
in the current domain or path.Procedure

To read a cookie
Read a string from the Cookies collection using the cookie's name as the key.
The following example reads a cookie named UserSettings and then reads the value of the
subkey named Font.


if (Request.Cookies["UserSettings"] != null)
{
    string userSettings;
    if (Request.Cookies["UserSettings"]["Font"] != null)
    { userSettings = Request.Cookies["UserSettings"]["Font"]; }
}

How to: Delete a Cookie
You cannot directly delete a cookie on a user's computer. However, you can direct the
user's browser to delete the cookie by setting the cookie's expiration date to a past date.
The next time a user makes a request to a page within the domain or path that set the
cookie, the browser will determine that the cookie has expired and remove it.

To assign a past expiration date on a cookie
  • Determine whether the cookie exists, and if so, create a new cookie with the same name.
  • Set the cookie's expiration date to a time in the past.
  • Add the cookie to the Cookies collection object.
The following code example shows how to set a past expiration date on a cookie.


if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}

1 comment:

  1. đồng tâm
    game mu
    cho thuê nhà trọ
    cho thuê phòng trọ
    nhac san cuc manh
    số điện thoại tư vấn pháp luật miễn phí
    văn phòng luật
    tổng đài tư vấn pháp luật
    dịch vụ thành lập công ty trọn gói
    lý thuyết trò chơi trong kinh tế học
    đức phật và nàng audio
    hồ sơ mật dinh độc lập audio
    đừng hoang tưởng về biển lớn ebook
    chiến thắng trò chơi cuộc sống ebook
    bước nhảy lượng tử
    ngồi khóc trên cây audio
    truy tìm ký ức audio
    mặt dày tâm đen audio
    thế giới như tôi thấy ebook

    " Nếu vậy ngươi chịu chết đi!" Trong khi nói chuyện, nữ tử che mặt đã đâm kiếm tới, khí thế và uy lực thật hãi người.

    " Còn ít tuổi mà đã có tu vi như vậy, đáng khen." Lưu Phong khóe miệng nhẹ nhàng cười, ánh mắt ngưng trọng, đề thăng chân nguyên rung mạnh nhuyễn kiếm xuất ra, trong phút chốc hóa thành kiếm quang đầy trời đâm tới trước ngực nữ tử che mặt.
    Nữ nhân che mặt thấy Lưu Phong xuất kiếm, thế kiếm lợi hại, đôi mắt đẹp không giấu được vẻ kinh ngạc, rồi thì mày liễu dựng lên, mắt tóe hào quang, trường kiếm trong tay hoa lên, tạo nên một trận huyền quang nhằm mặt Lưu Phong đâm tới.

    Nhuyễn kiếm vung lên, nhằm hướng trường kiếm của nữ tử che mặt chém tới. Một tiếng Đinh vang lên, Lưu Phong chỉ cám thấy hổ khẩu tê dại, nhuyễn kiếm trong tay đã hóa giải được công kích của nữ tử che mặt, sau đó nhuyễn kiếm lay động, nhằm bộ ngực của nữ tử đâm tới.

    Tay phải cầm kiếm của nữ tử che mặt không tự chủ được dạt ra ngoài, đột nhiên nghĩ ra bộ ngực của mình nằm trong phạm vi ảnh hưởng của kiếm khí, không khỏi cảm thấy xấu hổ, thẹn thùng, đôi mắt đẹp phát lạnh, chân ngọc đạp một đạp, cả thân thể mềm mại bay nhanh ngược lại: “Vô sỉ, hạ lưu”

    Ta khinh, con mẹ ngươi định lấy mạng ta thì sao, ta bất quá chỉ đâm miu miu của ngươi, ngươi chính là thánh nhân, còn ta là hạ lưu à. Lưu Phong sắc mặt biến đổi, trong mắt sát ý hiện lên.

    ReplyDelete

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives