Asp. Description and usage of cookie status in net-practical tips

Source: Internet
Author: User

Cookies first appeared in Netscape Navigator 2.0. Later ASP also introduced this technology, its role is to identify users with the session object. Whenever a user starts to connect to a site, the system automatically creates a user-related session state in the memory block, and creates a user ID that is stored on the browser side and is uniquely connected to the current user. In this way, the server saves the session and the browser saves the Cookie (the user's ID). The next time a user makes a request, the requesting user will be asked to submit the user's ID, in contrast to restore the original session state correctly. This is the method of maintaining the user's flag in the context of HTTP without a status protocol.
You can write cookies directly to the browser through the RESPONSE.COOKIES.ADD () method, and read the cookies that have been set up by the Request.Cookies method.
The way to write cookies is to create a HttpCookie object that constructs a cookie from this object. For example:

Create a HttpCookie object 
HttpCookie cookie = new HttpCookie ("Le pig Net"); 
Set this cookie value 
cookie. Value = "Getting Started with programming website"; 
Add this cookie 
Response.Cookies.Add (cookie);

Cookies are temporary and permanent. Permanent cookies are stored as files on your computer and remain on your computer when you close Internet Explorer. When you visit the site again, the Web site that created the Cookie can be read. At a specific programming time, when writing this cookie, set the life cycle of the cookie, with the following code:

DateTime dtnow = DateTime.Now; 
TimeSpan Tsminute = new TimeSpan (0, 1, 0, 0); 
Cookie. Expires = Dtnow + tsminute; 
RESPONSE.COOKIES.ADD (cookie);

The code above is to set the lifetime of the newly generated cookie to one hours, and you can set the cookie specific lifetime by modifying the TimeSpan properties. If you do not set the time, the default time is 20 minutes.
Read the following statement when reading the specified Cookie:

HttpCookie cookie = request.cookies["cookie name"];

If you want to display a read-out Cookie, you can use the following statement:

Response.Write (Cookies. Value.tostring ());

A Cookie is a string stored on the client, which affects the user's behavior, but is not directly managed by the user, although it is simply a flag (alphanumeric string) rather than a program, and it is not possible to use it to collect user information and destroy the user's privacy. But some users still do not trust, may be unwilling to occupy their own space, a considerable number of users in the browser prohibit the use of cookies. This brings difficulties in identifying users.
ASP.net 2.0 has now completely solved the method of identifying users without using cookies (ASP.net 1.1 only partially solves this problem). The solution is simply to configure the <sessionState> node in the Web.config file in the root directory of the application, and no other program needs to be modified. Why do you have to configure it in the root directory of your application? Because the settings for session state are application-scoped settings. Web pages in the site are either all using the configuration or none. The configured statements are:

<sessionstate cookieless= "UseUri"/>

Or

<sessionstate cookieless= "AutoDetect"/>

When you configure, when you write to the "cookieless=" statement, the AutoDetect, UseCookies, UseDeviceProfile, UseUri Four choices are popped up. 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, in the case of cookies, you simply set the cookieless property.
How does the system identify the user without a Cookie? Originally, when the previous settings, the system will require users to automatically embed the client's resource information in the user's set of URL statements. For example, when using cookies, the URL for a user to set up a Web page is: http://yourserver/folder/default.aspx, and now you have set up a configuration that does not use cookies, the URL of the invoked statement becomes: http:// yourserver/folder/(here in session ID)/default.aspx, where "session ID" represents the location of the user's resource information. The information has been inserted into the statement of the URL. Because user resource information is unique to the user, it can be used to combine with the session object to identify the user.

Here is a complete small example, please see 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);//20 seconds of validity 
  Response.Cookies.Add (CK); 
  Response.Write ("New CK"); 
} 
else
{ 
  Response.Write (CK). Value.tostring ()); 
} 
   
Store multiple information in a 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 about the cookie status in the ASP.net and usage, for the use of cookies and the pros and cons of the argument, we want to use cookies reasonably hope this article is helpful to learn cookies.

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.