Description and usage of Cookie status in ASP. NET, asp. netcookie

Source: Internet
Author: User

Description and usage of Cookie status in ASP. NET, asp. netcookie

The Cookie first appeared in Netscape Navigator 2.0. Later, ASP introduced this technology. Its role was to combine with Session objects to identify users. When a user starts to connect to the site, the system will automatically create a user-related session Status in the memory block, and at the same time create a user ID to store on the browser side, it is uniquely associated with the current user. In this way, the server saves the Session and the browser saves the Cookie (User ID ). When the current user sends a request, the requested user is requested to submit the user ID, and the two are compared to correctly restore the original session status. This is the way to keep the user flag under the HTTP condition of the stateless protocol.
You can use the Response. Cookies. Add () method to directly write Cookies to the browser and use the Request. Cookies method to read the configured Cookies.
The Cookie writing method is to create an HttpCookie object to construct a Cookie. For example:

// Create an HttpCookie object HttpCookie cookie = new HttpCookie ("Le pig"); // set the Cookie value. value = "programming Portal"; // Add this Cookie Response. cookies. add (cookie );

Cookies are temporary and permanent. Permanent cookies are stored on the computer as files, and are retained on the computer when Internet Explorer is disabled. When you access the site again, the website that creates the Cookie can read it. During specific programming, the lifecycle of the Cookie is set when the Cookie is written. The Code is as follows:

DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0); cookie.Expires = dtNow + tsMinute; Response.Cookies.Add(cookie);

The above Code sets the life cycle of the new Cookie to an hour. You can modify the TimeSpan attribute to set the specific life cycle of the Cookie. If no time is set, the default time is 20 minutes.
The statement used to read the specified Cookie is as follows:

HttpCookie cookie = Request. Cookies ["Cookie name"];

To display the read Cookie, use the following statement:

Response.Write(cookie.Value.ToString());

Cookie is a string stored on the client. It affects user behavior, but is not directly managed by the user. Although it is only a sign (a string of letters and numbers) rather than a program, it cannot be used to collect user information and undermine user privacy. However, some users are still uneasy, and may be reluctant to take up their own space. A considerable number of users are prohibited from using cookies in browsers. This makes it difficult to identify users.
ASP. NET 2.0 has completely solved the problem of identifying users without using cookies (ASP. NET 1.1 only partially solves this problem ). The solution is simple. You only need to configure the <sessionState> node in the Web. config file under the root directory of the application. No other program needs to be modified. Why must I configure it in the root directory of the application? Because the session status is set in the application scope. All webpages on the site either use this configuration or do not use this configuration. The configuration statement is:

<sessionState cookieless="useUri" />

Or

<sessionState cookieless="AutoDetect" />

When you compile the cookieless = Statement, four options are displayed: AutoDetect, useCookies, useDeviceProfile, and useUri. You can select AutoDetect or useUri to identify users without Cookies.
Although other aspects of session state management can be configured in the <sessionState> node, including storage media and connection strings, you only need to set the Cookieless attribute for cookies.
How does the system identify users without cookies? After the preceding settings are completed, the system will require the user to automatically embed the client resource information into the URL statement used by the user. For example, when a user uses cookies, the URL of a webpage is http: // yourserver/folder/default. aspx, now the Cookie-free configuration is set, the URL of the called statement will be changed to: http: // yourserver/folder/(session ID here)/default. aspx, where "session ID here" represents the location of the user's resource information. This information has been inserted into the URL statement. Because the user resource information is unique to the user, it can be combined with the Session object to identify the user.

Here is a complete small example. Please refer to the following source code:

HttpCookie ck = Request. cookies ["cktest"]; if (ck = null) {ck = new HttpCookie ("cktest"); ck. value = "123"; ck. expires = DateTime. now. addSeconds (20); // The validity period of 20 seconds Response. cookies. add (ck); Response. write ("new ck");} else {Response. write (ck. value. toString ();} // store multiple information in one Cookie HttpCookie cookie = new HttpCookie ("cktest"); cookie. values. add ("v1", "1"); cookie. values. add ("v2", "2"); cookie. values. add ("v3", "3"); Response. appendCookie (cookie); HttpCookie cookies = Request. cookies ["cktest"]; string value1 = cookies. values ["v1"]; string value2 = cookies. values ["v2"]; Response. write (value1 + value2 );

The above is the description and usage of the Cookie status in ASP. NET. There are different advantages and disadvantages of Cookie use. We need to use cookies reasonably and hope this article will help you learn about 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.