Explanation and usage of Cookie state in ASP. NET

  • 2021-07-02 23:55:59
  • OfStack

Cookie first appeared in Netscape Navigator 2.0. Later, ASP also introduced this technology, which is combined with Session objects to identify users. Whenever a user starts to connect to the site, the system will automatically create a user-related session state in the memory block, and at the same time create a user's ID to be stored in the browser, which is uniquely connected with the current user. Thus, the server saves Session and the browser saves 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, and compare the two to correctly restore the original session state. This is the way to keep the user flag under the HTTP condition of stateless protocol.
You can write Cookie directly to the browser through the Response. Cookies. Add () method, and read the set Cookie through the Request. Cookies method.
The way to write Cookie is to create an HttpCookie object and construct an Cookie from this object. For example:


// Create 1 A  HttpCookie  Object  
HttpCookie cookie = new HttpCookie(" Music pig net "); 
// Set this  Cookie  Value  
cookie.Value = " Programming portal "; 
// Join this  Cookie 
Response.Cookies.Add(cookie);

Cookie has temporary and permanent ones. The permanent Cookie is stored on your computer as a file and remains on your computer when you shut down Internet Explorer. When you visit the site again, the website where the Cookie was created can be read. In the specific programming time, when writing this Cookie, set the life cycle of Cookie, and its 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 newly generated Cookie to 1 hour, and the specific life cycle of Cookie can be set by modifying the attributes of TimeSpan. If no time is set, the default time is 20 minutes.
The statement when reading the specified Cookie is as follows:


HttpCookie cookie = Request.Cookies["Cookie  Name of "];

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


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

Cookie is a string stored in the client, which will affect the user's behavior, but it is not directly managed by the user. Although it is only a sign (alphanumeric string) rather than a program, it is impossible to use it to collect user's information and destroy user's privacy. However, some users are still not at ease, and may not want others to occupy their own space. A considerable part of users prohibit the use of Cookie in browsers. This makes it difficult to identify users.
ASP. NET 2.0 now completely solves the method of identifying users without Cookie (ASP. NET 1.1 only partially solves this problem). The solution is very simple, just in the Web. config file under the root directory of the application, the < sessionState > Node is configured, and no other programs need to be modified. Why must 1 be configured under the root directory of the application? Because the session state settings are application-wide. Either all pages in the site use this configuration or none of them use it. The configured statement is:


<sessionState cookieless="useUri" />

Or


<sessionState cookieless="AutoDetect" />

During configuration, when writing the statement "cookieless=", four choices of AutoDetect, useCookies, useDeviceProfile and useUri will pop up. Selecting AutoDetect or useUri can identify users without Cookies.
Although in < sessionState > Node can also configure other aspects of session state management, including storage media and connection strings, but in the case of Cookie, you only need to set the Cookieless property.
How does the system identify users without Cookie? Originally, after the previous setting, the system will require the user to automatically embed the client resource information into the URL statement set by the user. For example, in the case of using Cookie, when a user sets up a web page, the URL is: http://yourserver/folder/default.aspx. Now that the configuration without Cookie is set, the URL of the called statement will become: http://yourserver/folder/(session ID here)/default. aspx, where "session ID" represents the location of the user's resource information. This information has been inserted into the statement of URL. Since the user resource information is unique to the user, it can be used in combination 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 Validity period of seconds  
  Response.Cookies.Add(ck); 
  Response.Write("new ck"); 
} 
else
{ 
  Response.Write(ck.Value.ToString()); 
} 
   
// In 1 A Cookie Multiple information is stored in the  
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 ASP. NET in Cookie state description and usage, for Cookie use of advantages and disadvantages, we want to use Cookie reasonably, I hope this article is helpful for everyone to learn Cookie.


Related articles: