As cookies are often used in the development of B/s, ASP. NET must provide us with something ready to use.
This is the object: HttpCookie, of course, for ASP, there are also cookie objects in request and response.
First make clear what a cookie is:
Cookies are generated by the server and sent to User-agent (typically a browser), and the browser saves the key/value of the cookie to a text file in a directory, The next time you request the same Web site, the cookie is sent to the server (provided the browser is set to enable cookies). The cookie name and value can be defined by the server-side development itself.
So how do you use cookies under ASP.
The first is that there are two ways to create: asp:
The first is to manipulate the cookie object directly in the response object:
response.cookies["userName""Patrick"
response.cookies["userName"]. Expires = DateTime.Now.AddDays (1);
Since the existence of a key-value pair, we can directly create a key corresponding to the value, about the cookie expiration time of the problem we later study.
The second is to create a cookie object and add it to the Response object:
New HttpCookie ("lastvisit"== DateTime.Now.AddDays (1); RESPONSE.COOKIES.ADD (acookie);
This approach is recommended by individuals, although it seems that the code is a bit more, but the readability is relatively high.
The cookie eventually needs to be sent to the client to start using. After the cookie object is added to the response object, it must wait until useragent (typically the browser) resolves the request, and the cookie is written to the client, ie for example, and the cookie is written to the temporary Internet folder. Then the subsequent request cookie will be sent to the server.
Then we can really make use of cookies, there are many ways to manipulate cookies, which are recorded only on the server side, and for ASP. The cookies in the Request object record all the cookies sent, thus forming a set.
It is very common to use key-value pairs to take a value:
if (request.cookies["userName"null) { = request.cookies[" UserName " ]; string ctemp=Server.HTMLEncode (acookie.value); // string ctemp=request.cookies["UserName"]. values["name"];}
This is a similar way to believe that you have used countless times in C #, the only difference is that the value of the annotation in the way. Because in this case we have only one key-value pair stored in a cookie object, in fact a cookie object can have n key-value pairs, so, if there are multiple key-value pairs, it is not easy to use value to take values, of course, the above method can also get the specified value in more than one worth. The core is to use values in the cookie object rather than value.
OK, take the value, then do what we want to do, be brave, anything can ...
Then we will briefly discuss the expiry time of the cookie:
By default, the cookie expiration time is not set, and the cookie expires until the browser is closed, which is equivalent to clearing cookies when the session is closed. We can also set our own cookie expiration time:
response.cookies["userName"]. Expires = DateTime.Now.AddDays (1= DateTime.Now.AddDays (1=New TimeSpan (1000= dt. ADD (TS);
The above is the way to set the expiration time, in fact, the difference is not small, is to set the Expires property, the cookie is the effective time from the start of the calculation. Can be set flexibly depending on the situation. The time operation method is many, here the friendly hint, is not recommended to use: Response.Cookies ("field name"). expires= time function +n this way to set the expiration time, as mentioned in a message from a great God, this method will appear unable to display the page or 500 error, It is recommended to set the expiration time by using the Add function of the DateTime object.
Finally, another problem is that deleting Cookie:ASP.NET does not directly delete the Cookie on the user's computer. However, you can allow the user's browser to delete cookies by setting the expiration date for the cookie to a past date. The next time a user makes a request to a page within the domain or path where the cookie is set, the browser determines that the cookie has expired and is removed.
The Remove method that invokes the cookie collection removes the cookie from the server-side collection so that the cookie is not sent to the client. However, if the client already has a cookie, it needs to set the expiration time and the cookie will be deleted on the client after the request is sent to the client.
Mycookie.expires = DateTime.Now.AddDays (-1d); RESPONSE.COOKIES.ADD (MyCookie);
The above code is how you set the cookie to expire.
Reminder: Cookie operation recommended that you try again, the cookie itself belongs to a certain risk of operation, because the data will be kept in the client for a long time, in general, the use of key data such as user name password will be selected encrypted storage.
Basic use of cookies under ASP.