Basic knowledge of Cookie programming in ASP. NET (1)

Source: Internet
Author: User

Introduction

Cookie provides a useful method for Web applications to save user-related information. For example, when a user accesses your site, you can use cookies to save user preferences or other information, so that when the user visits your site next time, the application can retrieve previously saved information.

This article describes the application of cookies in ASP. NET applications. It shows you the technical details of Cookie application in ASP. NET, such as compiling cookies and then reading them. In addition, we will introduce you to various features and special circumstances of cookies, as well as ASP. NET's support for cookies.

What is Cookie?

Cookie is a short text message that is transmitted between the Web server and the browser as user requests and pages. Each time a user accesses a site, the Web application can read the information contained in the Cookie.

Assume that the user requests to access your websiteWww.contoso.comWhen your application sends a page to the user, 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 saves it in a folder on the user's hard disk.

Later, if the user visits the page on your site again, when the user enters the URLWww.contoso.comThe browser will find the Cookie associated with the URL on the local hard disk. If the Cookie exists, the browser sends it along with the page request to your site, and your application can determine the date and time of the user's last visit to the site. You can send a message to the user based on this information, or check the expiration time or perform other useful functions.

Cookie is associated with a Web site rather than a specific page. Therefore, no matter which page the user requests to browse the site, the browser and server will exchangeWww.contoso.com. When a user accesses other sites, each site may send a Cookie to the user's browser, and the browser will save all these cookies separately.

These are the basic operating principles of cookies. So what are the functions of cookies? The most fundamental purpose is that cookies can help websites save information about visitors. To put it more simply, Cookie is a way to maintain the continuity of Web applications (that is, to execute "state management. The browser and the Web server are always disconnected except for a short period of actual information exchange, and each request sent by the user to the Web server is processed independently, regardless of all other requests. However, in most cases, it is necessary for the Web server to identify you when you request a page. For example, a Web server on a shopping site tracks each shopper so that the site can manage shopping cart and other user-related information. Therefore, the Cookie function is similar to a business card. It provides the relevant identification information to help the application determine how to proceed.

Cookie can be used for multiple purposes, all of which are to make the Web site remember you. For example, a site that implements a public opinion test can simply use cookies as a Boolean value to indicate whether your browser has participated in the vote, thus avoiding repeated voting; websites that require user login can use cookies to determine whether you have logged on, so that you do not have to enter creden。 every time.

For more background information about Cookies, we recommend that you read the article "How Internet Cookies Work" on the Verizon Web site. The address isHttp://www22.verizon.com/about/community/learningcenter/articles/displayarticle1/0,4065,1022z1,00.html(English ). The author explained in detail what cookies are and how they exchange information between browsers and servers. He also summarized the privacy issues related to cookies.

By the way, do you want to know why they are called "cookies "? Jargon File (also known as The New Hackers Dictionary) version 4.3.3 provides an accurate definition and reasonable explanation of The term. You canHttp://www.catb.org /~ Esr/jargon/jargon.html # cookieFind related entries.

In the subsequent content, this article assumes that you already know what a Cookie is and that you know why to use a Cookie in an ASP. NET application.

Cookie restrictions

Before discussing the technical details of cookies, I would like to introduce several restrictions on Cookie applications. Most browsers support up to 4096 bytes of cookies. If you want to save a few values to your computer, this space is large enough, however, you cannot use a Cookie to save a dataset or a large amount of other data. In practice, you may not want to save a large amount of user information in cookies, but only want to save user numbers or other identifiers. Then, when the user visits your site again, you can use this user ID to search for the user's details in the database. (For information on saving user information, see cookies and security .)

The browser also limits the number of cookies that your site can save on your computer. Most browsers only allow 20 cookies to be saved on each site. If you try to save more cookies, the first Cookie will be deleted. Some browsers also limit the total number of cookies from all sites, which is usually 300.

The most likely Cookie restriction is that users can set their own browsers to reject cookies. It is difficult for you to solve this problem unless you do not use cookies at all but use other mechanisms to save user-related information. A common method for saving user information is session status, which depends on cookies. This is described in the Cookie and session status.

Note: For more information about status Management and options used to save information in Web applications, see Introduction to Web Forms State (English) and State Management Recommendations (English ).
Even though cookies are useful in applications, applications should not rely on the ability to save cookies. Cookies can be icing on the cake, but do not use them to support key functions. If your application must use cookies, you can test to determine whether the browser accepts cookies. I briefly introduced a test method in the section "check whether the browser accepts cookies" after this article.


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.