[Stick to top] elaborate on cookies

Source: Internet
Author: User

What is cookies?

Cookies are small files created in the client system or in the memory of the client browser (if temporary ). It enables status management. We can store a small amount of information to systems that can be short for use as needed. The most interesting thing is that it is transparent to users. In your Web ApplicationProgramYou can use it everywhere, which is extremely simple. Cookies are stored in text format. If a web application uses cookies, the server sends cookies and the client browser stores them. On the next request page, the browser returns cookies to the server. The most common example is to use a cookie to store user information, user preferences, and "Remember password" operations. Cookies have many advantages and, of course, many disadvantages. I will talk about it later.

How is cookies created?

When a client sends a request to the server, the server sends cookies to the client. The same cookies can be used by subsequent requests. For example, if codeproject.com stores session IDs as cookies. When a client requests a page to the Web server for the first time, the server generates a session ID and sends it as cookies to the client.

Now, all subsequent requests from the same client will use the session ID from cookies, as shown in the following figure.

The browser and the Web server exchange cookies for response. For different sites, the browser maintains different cookies. If a page requires information in cookies, when a URL is "clicked", the browser first searches for the cookies of the local system and then forwards the information to the server for information.

Cookies

The following are the main advantages of using cookies:

(1) Implementation and use are very simple

(2) the browser is responsible for maintaining the sent data (cookies)

(3) the browser automatically manages cookies from multiple sites.

Cookies

The following are the main disadvantages of cookies:

(1) It stores data in simple text format, so it is not safe at all.

(2) Cookies are limited in size (4 kb)

(3) the maximum number of cookies is also limited. Mainstream browsers can limit the number of cookies to 20. If new cookies arrive, the old ones will be deleted. Some browsers support up to 300 cookies.

(4) We need to configure the browser. Cookies will not work in the high-security environment configured by the browser.

How to create cookies

To use cookies, we first need to introduce the namespace system. Web:

Using system. Web;

See the followingCodeTo see how we create cookies and add them to the Web response:

After cookies are created, they will continue to be closed in the browser. We can make it last longer. How can this problem be achieved? I will explain it later

How to read information from cookies

Before reading a cookie, we must first check whether the cookie can be found. Before reading cookies, check that they are always a good habit, because the browser may disable cookies.

What are persistent and non-persistent cookies?

Cookies can be divided into two categories:

(1) Persistent cookies

(2) non-persistent cookies

Persistent cookies: This can be called a permanent cookie, which is stored in the client's hard disk until they expire. Persistent cookies should be set with an expiration time. Sometimes they exist until the user deletes them. Persistent cookies are usually used to collect user identification information for a system.

Non-persistent cookies: It can also be called temporary cookies. If no expiration time is defined, the cookie will be stored in the browser memory. The example shown above is a non-persistent cookie.

Modifying a persistent cookie is no different from a non-persistent cookie. The only difference between them is that persistent cookies have an expiration time setting.

How to Create persistent cookies

I have provided an example of creating a non-persistent cookie. For persistent cookies, we need to define an expiration time for them. In the following code, I define a persistent cookie because I mark its expiration time as 5 days.

// Creting a cookie object <br/> httpcookie _ userinfocookies = new httpcookie ("userinfo "); </P> <p> // setting values inside it <br/> _ userinfocookies ["username"] = "Abhijit "; <br/> _ userinfocookies ["usercolor"] = "red"; <br/> _ userinfocookies ["expire"] = "5 days "; </P> <p> // Adding expire time of cookies <br/> _ userinfocookies. expires = datetime. now. adddays (5); </P> <p> // Adding cookies to current web response <br/> response. cookies. add (_ userinfocookies );

The most interesting thing is where they are stored on the hard disk?

Where are Cookies stored on the local hard disk?

One interesting thing is to know where cookies are on your hard disk. First, go to the Browse folder option and select "show hidden files and folders ".

Now, navigate to the current user's cookies folder. Take a look at the figure below:

How to remove cookies before the expiration time

This is an interesting task. If you want to remove persistent cookies before the expiration time, the only way is to use the past time as the expiration time to replace the old expiration time.

Httpcookie _ userinfocookies = new httpcookie ("userinfo"); <br/> // Adding expire time of cookies before existing cookies time <br/> _ userinfocookies. expires = datetime. now. adddays (-1); <br/> // Adding cookies to current web response <br/> response. cookies. add (_ userinfocookies );

How to Control cookies

We can control the cookie domain in the following ways:

(1) restrict cookie paths

(2) restrict cookie Domains

What is Cookie munging?

By default, Asp.net uses cookies to store session IDs, but as I mentioned earlier, some Browsers Do not support cookies. To overcome this problem, Asp.net uses the "cookies munging" technology to manage session variables instead of cookies.

Why do we use cookies munging in Asp.net?

There are some special reasons for using cookies munging in Asp.net:

(1) Some browsers do not support cookies

(2) Sometimes users allow browsers to disable cookies

How does cookie munging work?

When a user requests a page from the server, the server codes the "session ID" and adds them to each href link on the page. When a user clicks a link, Asp.net decodes the session ID and sends it to the user's requested page. Now, any session variables can be retrieved on the requested page. This will occur smoothly-When Asp.net detects that the user's browser does not support cookies.

How to Implement cookie munging

To achieve this, we have to turn the session status into non-cookies:

<Sessionstate cookieless = "True/>

How to configure cookies in a browser

Now let's take a look at how to configure in a browser-enable or disable cookies. I have discussed the settings of IE browser before. Click Tools> Internet Options> to go to the privacy tab. Here, you can see a scroll bar like the following:

The first option will allow all cookies, and the last option will block all cookies. You can get more information during scrolling.

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.