Cookie is a short text message that is transmitted between the web server and the browser as user requests and pages. Cookie contains information that can be read by web applications every time a user accesses the site.
This topic includes:
Solution
Background
Sample code
Class reference
Other resources
New Feature
Solution
--------------------------------------------------------------------------------
Cookie provides a way to store user-specific information in web applications. For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user visits your website again, the application can retrieve the previously stored information.
Back to top
Background
--------------------------------------------------------------------------------
Cookie is a short text message that is transmitted between the web server and the browser as user requests and pages. Cookie contains information that can be read by web applications every time a user accesses the site.
For example, if the application sends a page to the user when the user requests the page in the site, it is not only a page, but also a cookie containing the date and time, the user's browser obtains the cookie while obtaining the page, and stores it in a folder on the user's hard disk.
Later, if the user requests the page on your site again, when the user enters the url, the browser will find the cookie associated with the url on the local hard disk. If the cookie exists, the browser sends the cookie along with the page request to your site. Then, the application can determine the date and time of the user's last visit to the site. You can use this information to display a message to the user or check the expiration date.
Cookies are associated with websites rather than specific pages. Therefore, no matter which page on the site the user requests, the browser and server exchange cookie information. When a user accesses different sites, each site may send a cookie to the user's browser, which stores all the cookies separately.
Cookies help websites store information about visitors. In general, cookie is a way to maintain the continuity of web applications (that is, execution status management. Except for a short period of actual information exchange time, the browser and the web server are disconnected. The web server processes each request sent to the web server separately. However, in many cases, it is very useful for web servers to identify users on user request pages. For example, a web server on a shopping site tracks each shopper so that the site can manage the shopping cart and other user-specific information. Therefore, cookie can be used as a business card to provide relevant identification information to help the application determine how to continue execution.
Cookie can be used for multiple purposes, all of which are to help websites remember users. For example, a site that implements a public opinion test can simply use cookies as a boolean value to indicate whether the user's browser has participated in the vote, so that the user cannot perform a second vote. Websites requiring users to log on can use cookies to record that users have logged on, so that users do not have to enter creden。 every time.
Cookie restrictions
Most browsers support a cookie of up to 4096 bytes. Because of this small limit, cookies are best used to store a small amount of data or user IDs. User IDs can then be used to identify users and read user information from database tutorials or other data sources. (For information about how to store user information security suggestions, see the "cookie and security" section below .)
The browser also limits the number of cookies that the site can store on the user's computer. Most browsers only allow 20 cookies to be stored on each site. If you try to store more cookies, the oldest cookies will be discarded. Some browsers impose absolute limits on the total number of cookies they will accept from all sites, typically 300.
The cookie restriction you may encounter is that users can set their browsers to reject cookies. If you define an p3p privacy policy and place it in the root directory of the website, more browsers will accept the cookies of your site. However, you may have to discard cookies completely and store user-specific information through other mechanisms. The common method for storing user information is session status, but session status depends on cookie, which is described in the "cookie and session status" section later.
Description
For more information about state management and options for saving information in web applications, see asp tutorial. net state management overview and asp.net tutorial state management recommendations.
Although cookies are very useful in applications, applications should not rely on the ability to store cookies. Do not use cookies to support key functions. If your application depends on cookies, you can test to determine whether the browser will accept cookies. See "check whether your browser accepts cookies" next to this topic.
Compile cookie
The browser manages cookies in the user's system. Cookie is sent to the browser through the httpresponse object, which is known as a set of cookies. The httpresponse object can be accessed as the response attribute of the page class. All cookies to be sent to the browser must be added to this collection. When creating a cookie, you must specify the name and value. Each cookie must have a unique name so that it can be recognized when being read from the browser. Because cookies are stored by name, naming two cookies with the same name causes one cookie to be overwritten.
You can also set the cookie expiration date and time. When a user accesses the website where the cookie is written, the browser deletes the expired cookie. As long as the application considers the cookie value valid, the cookie validity period should be set to this period. For cookies that never expire, you can set the expiration date to 50 years from now on.
Description
Users can clear cookies on their computers at any time. Even if the stored cookie has a long expiration date, you can still decide to delete all the cookies and clear all the settings stored in the cookie.
If no cookie validity period is set, a cookie will still be created, but it will not be stored on the user's hard disk. The cookie is maintained as part of the user's session information. When the user closes the browser, the cookie is discarded. This kind of non-permanent cookie is suitable for saving the information stored for a short period of time, or saving the information that should not be written to the disk on the client computer for security reasons. For example, if you are using a public computer and you do not want to write cookies to the disk of the computer, you can use non-permanent cookies.
The following code example demonstrates a cookie named usersettings and sets the values of its font and color subitem. It also sets the Expiration Time to tomorrow.
Response. cookies ["usersettings"] ["font"] = "arial"; response. cookies ["usersettings"] ["color"] = "blue"; response. cookies ["usersettings"]. expires = datetime. now. adddays (1d );
Compile a cookie by creating an instance of an httpcookie object
Create an httpcookie object and assign it a name.
Assign values to the cookie subitem and set all cookie attributes.
Add the cookie to the cookies set.
The following code example demonstrates an instance of an 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 );