Document directory
- . Net basics: Introduction to Cookie read/write methods in ASP. NET
[Taken from Jun. Net's
Blog]. NET provides two cookie classes: system. Web. httpcookie and system. net. Cookie.
Cookie collection class: system. Web. httpcookiecollection class and system. net. cookiecollection class.
The differences between them are as follows:
The system. Web namespace is used for server segments, and system. NET is used for client programs.
In fact, there are more than the following differences:
Next we will compare the attributes of these two cookie classes as follows. These attributes are all copied from the msdn Chinese Version instructions:
System. Web. httpcookie class |
System. net. Cookie class |
Description of the constructor in msdn: Overloaded. Initializes a new instance of the httpcookie class. |
Description of the constructor in msdn: Overloaded. Initialize a new instance of the cookie class according to Netscape specifications. Generally, applications do not need to construct cookie classes because they are automatically created based on the set-Cookie header received through the HTTP response. |
|
Comment Obtains or sets the comments that the server can add to the cookie. |
|
Commenturi Obtain or set the URI comments provided by the server through cookies. |
|
Discard Gets or sets the discard flag set by the server. |
Domain Obtain or set the domain associated with the cookie. |
Domain Obtain or set the valid URI of a cookie. |
|
Expired Obtains or sets the current status of the cookie. |
Expires Obtain or set the cookie expiration date and time. |
Expires Obtain or set the cookie expiration date and time as datetime. |
|
HTTPOnly Determine whether the page script or other activity content can access this cookie. |
Name Obtain or set the cookie name. |
Name Obtain or set the cookie name. |
Path Obtain or set the virtual path to be transmitted with the current cookie. |
Path Obtain or set the URI that this cookie applies. |
|
Port Obtain or set the list of TCP ports that this cookie applies. |
Secure Gets or sets a value that indicates whether to use Secure Sockets Layer (SSL) (that is, to transmit cookies only through https. |
Secure Obtains or sets the cookie security level. |
|
Timestamp Obtain the time when this cookie is issued as datetime. |
Value Obtain or set a single cookie value. |
Value Obtain or set the cookie value. |
Values Obtains the set of key-value pairs contained in a cookie object. |
|
|
Version Obtain or set the HTTP status maintenance version that meets the cookie. |
You will see how good the system. net. Cookie is compared to the system. Web. httpcookie class. Some of our web developers do not know the attributes. Why?
This should begin with the cookie specification. There are currently the following cookie specifications:
◆ Netscape cookie draft
The earliest cookie specification is based on rfc2109. Although this specification is significantly different from rc2109, many servers are compatible with it.
◆ Rfc2109
The first official cookie specification released by W3C. Theoretically, all servers must follow this rule when processing cookies (version 1. Unfortunately, this specification is so strict that many servers improperly implement it or are still using the Netscape specification.
◆ Rfc2965 Specification
Cookie version 2 is defined, and the shortcomings of cookie version 1 are described.
Rfc2965 specification is currently used in a small number. Rfc2109 specifications are much stricter. In actual applications, not all browsers and web servers strictly abide by them. Therefore, the draft Netscape cookie is a simple and widely supported cookie specification.
Let's look at the differences between the system. Web. httpcookie class and the system. net. Cookie class.
The difference I understand is:
System. Web. httpcookie class
This category was initially designed for use by Web Servers because Microsoft's Web servers did not comply with rfc2109 \ rfc2965 specifications. Instead, it adopts the Netscape cookie draft scheme.
At the same time, in order to take into account some of the previous ASP encoding habits, so we have this class design.
As mentioned earlier, traversal of system. Web. httpcookiecollection may be written as follows:
foreach (string name in Request.Cookies) { info += string.Format("{0} = {1} \r\n ", name, Request.Cookies[name].Value); } |
Foreach (httpcookie cookie in request. Cookies) may fail. Why does Microsoft have such a design.
System. net. Cookie class
This class should be initially designed for use by clients. Because some server cookies follow the rfc2109 \ rfc2965 specification, the design of this class has more attributes.
========================================================== ======================================
. Net basics: Introduction to Cookie read/write methods in ASP. NET
Cookie (httpcookie instance) provides a method to store user-specific information in Web applications. For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user visits your website again, the application can retrieve the previously stored information.
Cookie in ASP. NET: Method for creating a cookie (1)
Response. Cookies ["username"]. value = "admin ";
Response. Cookies ["username"]. expires = datetime. Now. adddays (1 );
// If no expiration time is set, the cookie information will not be written to the user's hard disk, and will be discarded if the browser is closed.
Cookie in ASP. NET: Method for creating a cookie (2)
Httpcookie acookie = new httpcookie ("lastvisit ");
// Last access time
Acookie. value = datetime. Now. tostring ();
Acookie. expires = datetime. Now. adddays (1 );
Response. Cookies. Add (acookie );
Cookie in ASP. NET: Access cookie method (1)
If (request. Cookies ["username"]! = NULL)
Label1.text = server. htmlencode (request. Cookies ["username"]. value );
Cookie Access Method (2)
If (request. Cookies ["username"]! = NULL ){
Httpcookie acookie = request. Cookies ["username"];
Label1.text = server. htmlencode (acookie. value );
}
Cookie in ASP. NET: Method for creating multi-value cookies (1)
Response. Cookies ["userinfo"] ["username"] = "admin ";
Response. Cookies ["userinfo"] ["lastvisit"] = datetime. Now. tostring ();
Response. Cookies ["userinfo"]. expires = datetime. Now. adddays (1 );
Cookie in ASP. NET: Method for creating multi-value cookies (2)
Httpcookie acookie = new httpcookie ("userinfo ");
Acookie. Values ["username"] = "admin ";
Acookie. Values ["lastvisit"] = datetime. Now. tostring ();
Acookie. expires = datetime. Now. adddays (1 );
Response. Cookies. Add (acookie );
Cookie in ASP. NET: Read multi-value cookie
Httpcookie acookie = request. Cookies ["userinfo"];
String username = acookie. Values ["username"];
String lastvisit = acookie. Values ["lastvisit"];
Cookies in ASP. NET: Modify and delete cookies
You cannot directly modify or delete a cookie. You can create only one new cookie and send it to the client to modify or delete the cookie.
========================================================== ========================================
Cookie
Cookie usage is similar to ASP. For example, we create a cookie named aspcn with the value of flying knife.
Httpcookie = new httpcookie ["aspcn"];
Cookie. value = "Flying knife ";
Response. appendcookie (cookie );
It's easy to retrieve the cookie value.
Httpcookie cookie = request. Cookies ["aspcn"];
Cookievalue = cookie. value;
Sometimes we want to store multiple pieces of information in a cookie. For example, we add multiple information under the cookie named aspcn.
Httpcookie cookie = new httpcookie ("aspcn ");
Cookie. Values. Add ("webmaster", "Flying knife ");
Cookie. Values. Add ("Writer", "beige ");
Cookie. Values. Add ("linkcolor", "blue ");
Response. appendcookie (cookie );
Retrieving information is as easy as possible
Httpcookie cookie = request. Cookies ["aspcn"];
Value1 = cookies. Values ["webmaster"];
Value2 = cookies. Values ["Writer"];
View state
This is a new concept. Its usage is the same as session. Its main purpose is to record the status of web control. Although it is new, it is no different from the application and session usage, so I don't want to explain it in detail.
State ["droploadindex"] = 0;
The basic usage is as follows. This is useless after it is released, because it only saves the status of the web control.
ASP. NET advanced applications
The basic part of ASP. NET has been explained, but this is not the end of ASP. NET functions, there are a lot of powerful ASP. NET new features.
Web Service
Track
Internationalization
Cache
Component creation
Parts Control
There are still many other things. I can't just talk about it in one breath. Our next topic is the ASP. NET advanced application series. Close access to ASP. NET focuses on the basics :)