Asp.net built-in object Cookies (Introduction/attribute methods/basic operations and instances)

Source: Internet
Author: User
Tags http cookie

1. Understand Cookies

Cookies are a collection of data managed by Web servers stored on customers' computers. The data is related to the client and server. That is to say, each time a customer's browser logs on to a website, information related to the website is saved in Cookies. Even if a customer logs on to multiple websites using the same browser, the information of the browser and multiple websites will still be saved in Cookies. However, the information in Cookies is managed in an orderly manner, when the customer's browser logs on to a website again, only the corresponding information in Cookies will function.

Cookies are a very important technology in Web application design. When a Web server wants to know relevant information of a user or data transmitted between several ASP. NET files, it can use Cookies.

The Cookies with the specified name can be read out in the hosts file. Writing information in Cookies is completed by the Response object, and reading the Cookies is completed by the Request object. [In another article: Asp.net built-in Object Request object]

Ii. attributes and methods of Cookie objects

Attribute:
(1). Name: Get or set the Cookie Name
(2). Value: Get or set the Cookie Value
(3). Expires: Get or set the Cookie expiration time
(4). Version: Get or set the Version that meets the HTTP maintenance status of the Cookie.

Method:
(1). Add: Add a Cookie variable to save the specified cookie to the Cookies set.
(2). Clear: Clear the variables in the Cookie set.
(3). Get: Get the Cookie variable value through the variable name or index.
(4). Remove: delete a Cookie object using the Cookie variable name or index.

3. Basic Cookie operations

1. Create a Cookie object and set the expiration time
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
// Create a Cookie object
HttpCookie mycookie = new HttpCookie ("MyCookie"); // create a Cookie named "MyCookie"
Mycookie. Value = Server. HtmlEncode ("Hello everyone, I am a Cookie"); // set the Cookie Value
Mycookie. Expires = DateTime. Now. AddDays (10); // set the Cookie expiration time
Response. AppendCookie (mycookie); // Add an HTTP Cookie to an internal Cookie set
// Response. Cookies. Add (mycookie); // Add it to the internal Cookie set, which is the same
}

2. Get Cookie objects
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
// Obtain the Cookie object
Try
{
HttpCookie mycookie = new HttpCookie ("MyCookie ");
Mycookie. Value = Server. HtmlEncode ("Hello, I am a Cookie ");
Mycookie. Expires = DateTime. Now. AddHours (10 );
Response. AppendCookie (mycookie );
Response. Write ("Cookie created successfully ");
Response. Write ("// ---------- Use ------------
HttpCookie getMyCookie = Request. Cookies ["MyCookie"]; // get Cookie
Response. Write (getMyCookie. Name + getMyCookie. Value + getMyCookie. Expires); // output
}
Catch
{
Response. Write ("Cookie creation failed ");
}
}

Iv. instance: writing and reading cookies

The Response object contains a cookie attribute. You can set and manage Cookies through Cookies. If the specified cookie does not exist, the cookie is created. If yes, the old value is replaced with the new value.

Cookie objects must be redefined using the HttpCookie class provided by. NET. Use "Response. Cookies. Add" to send and save the information to the browser of the client. The cookie information can be read using the method provided by the Request object.

Example:

The Code is as follows:
Copy codeThe Code is as follows:
On the default1 page:
Protected void Page_Load (object sender, EventArgs e)
{

}
Protected void button#click (object sender, EventArgs e)
{
// Save cookie Information

HttpCookie c1 = new HttpCookie ("user"); // The HttpCookie class instantiates a cookie object, creates and names a new cookie.
C1.Value = "cookie Value"; // set the Value of a single cookie
Response. Cookies. Add (c1); // Save the specified cookie to the Cookies set
Response. Write ("<script> alert ('saved successfully! ') </Script> ");

}
Protected void Button2_Click (object sender, EventArgs e)
{
// Submit page
Response. Redirect ("Default2.aspx ");
}

Copy codeThe Code is as follows:
On the default2 page:
Protected void button#click (object sender, EventArgs e)
{
// Read coookie
Response. Write ("cookie Name (Name):" + Request. Cookies ["user"]. Name + "<br/> ");
Response. Write ("cookie Value (Value):" + Request. Cookies ["user"]. Value + "<br/> ");

}

5. Advantages and Disadvantages of Cookie objects compared with Session and Application:

The following is a summary of ASP. NET 3.5 development technology.

Compared with Session and Application objects, Cookie can be used to persistently store user information. The Cookie is stored on the client, while the Session and Application are stored on the server. Therefore, the Cookie can be saved for a long time. Web applications can obtain client cookies for user identity authentication.

Asp.net contains two Cookie sets that are accessed through HttpRequest's Cookie set. Cookie is not a subclass of the Page class. Therefore, the usage method is different from Session and Application, which has the following advantages over Cookie:

1. You can configure the expiration time.
2. Simple: Cookie is a lightweight text-based structure, including simple key-value pairs
3. Data Persistence: because the data is saved to the client
4. No server resources: because it is stored on the local client

Disadvantages::

1. size limit:
2. Uncertainty: users may delete or disable cookies.
3. Security Risks: forge and modify

Last article: Cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.