ASP.net built-in objects cookies (Introduction/attribute methods/basic operations and examples) _ Practical Tips

Source: Internet
Author: User
Tags http cookie set cookie

One, know the Cookies object

Cookies are a collection of data that is managed by the Web server and stored in a client computer. This data is client, server-side related. That is to say, every time a client's browser logs on to a website, the client's browser and the site's information are saved in the cookie. Even if customers use the same browser to log more than one Web site, in the cookies will still save the browser and a number of web site information, but cookies in the management of the information is orderly, when the client browser to log on to a website, only cookies in the corresponding information will occur.

Cookies are a very important technique for Web application design, and cookies can be used when a Web server wants to know about a user's relevant information or the data that is transmitted between several asp.net files.

Web applications in many Web sites are made up of multiple asp.net files, to accomplish a particular function, it is required that some parameters be transferred between these asp.net files, which can be stored as cookies, so that when file 1 wants to transmit data to file 2 o'clock, the data in file 1 takes the parameter as a cookie Write to the Cookies.txt file, and then file 2 reads the cookie information from the Cookies.txt file with the specified name. The writing of information in cookies is done by response object, and the reading of cookies information is done by request object. [In another: ASP.net in the request object of a built-in object]

The properties and methods of the Cookie object

Property:
(1). Name: Gets or sets the names of cookies
(2). Value: Gets or sets the value of the cookie
(3). Expires: Gets or sets the expiration time of a cookie
(4). Version: Gets or sets the HTTP maintenance-compliant versions of cookies

Method:
(1). Add: Add cookie variable, save the specified cookie to the Cookies collection
(2). Clear: Clears the variables in the cookie collection
(3). Get: Gets the value of a cookie variable by variable name or index
(4). Remove: Deletes a cookie object from a cookie variable name or index

Third, the basic operation of cookies

1. Create a Cookie object and set the expiration time

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Creating a Cookie Object
HttpCookie MyCookie = new HttpCookie ("MyCookie");//Create a cookie called "MyCookie"
MyCookie. Value = Server.HTMLEncode ("Everyone good, I am a cookie");/Set Cookie values
MyCookie. Expires = DateTime.Now.AddDays (10);//Set Cookie expiration time
Response.appendcookie (MyCookie);//Add an HTTP cookie to the internal cookie collection
RESPONSE.COOKIES.ADD (MyCookie);//Add to internal cookie collection, same as above
}

2. Get Cookie Object
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Get 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 ("Create cookie Success");
Response.Write ("----------Use------------
HttpCookie Getmycookie = request.cookies["MyCookie"];//get Cookies
Response.Write (Getmycookie.name + getmycookie.value + getmycookie.expires);//Output
}
Catch
{
Response.Write ("Cookie creation failed");
}
}

Iv. Examples: Write and read cookies

The response object contains a cookie attribute. Cookies can be set up and managed by the Cookies property information. If the specified cookie does not exist, create the cookie, and replace the old value with the new value if it exists.

The cookie object needs to be redefined with the HttpCookie class provided by. Net. Use the "RESPONSE.COOKIES.ADD" to send and save information to the client's browser, using the method provided by the request object to read the cookie information.

The following example:

The code is as follows:

Copy Code code as follows:

In the DEFAULT1 page:
protected void Page_Load (object sender, EventArgs e)
{

}
protected void Button1_Click (object sender, EventArgs e)
{
Save Cookie Information

HttpCookie C1 = new HttpCookie ("user"); the//httpcookie class instantiates a cookie object, creates and names a fresh cookie
C1. Value = "Cookie value"; Set the value of a single cookie
RESPONSE.COOKIES.ADD (C1); Saves the specified cookie to the collection of cookies
Response.Write ("<script>alert") (' Save success! ') </script> ");

}
protected void button2_click (object sender, EventArgs e)
{
Submit Page
Response.Redirect ("default2.aspx");
}

Copy Code code as follows:

On the DEFAULT2 page:
protected void Button1_Click (object sender, EventArgs e)
{
Read Coookie
Response.Write ("Name of the cookie": "+ request.cookies[" user "). Name + "<br/>");
Response.Write (value): + request.cookies["user". Value + "<br/>");

}

the advantages and disadvantages of the cookie object compared with session and application

The following summary from: ASP.net 3.5 Development Technology Encyclopedia

Use cookies to persist user information, compared to session and application objects. Cookies are stored on the client, and session and application are stored on the server side, so cookies can be saved for a long time. A Web application can authenticate users by obtaining a cookie from the client.

The asp.net contains a collection of two cookies, accessed through a HttpRequest cookie collection, and is not a subclass of the page class, so using the method differs from session and application, compared to the advantages of their cookies:

1. Can configure Expiration time
2. Simple: Cookies are a lightweight, text-based structure that includes simple key-value pairs
3. Data persistence: Because it is saved to the client
4. No server resources: Because stored on the local client

disadvantages are as follows :

1. Size limit:
2. Uncertainty: Possible users to delete cookies or disable
3. Security risk: May forge the modification

Finally recommend a: 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.