Cookie is a good way to store customer data. Unlike sessions that can also be used to store customer data, session stores data on the server, cookies store data on the client. The most common cookie application we use is storing Forum information. When you log on to a forum you like, there are usually choices like "whether to save your login information". If you select this option, you do not need to fill in the login form when you log on to this forum next time, instead, you can log on directly.
In Asp.net, the process of using cookies is roughly as follows: Create an httpcookie instance, assign values to it, and add the instance that saves the information to the httpresponse cookies set, in this way, the pre-setting of the cookie is completed. To use the cookie, you need to extract the required cookie from the httprequest cookies set and then assign it to an httpcookie instance, then use it ...... In short, the specific process is production. It is better to save the following process.
Production:
// Create an httpcookie instance
Httpcookie pai_shun_hc = new httpcookie ("test ");
// Add data to the instance. The first string indicates the key value, which is used for marking. Just like a pile of boxes in the warehouse, each box has its own number, the second string is the relative value, just like what is stored in the box.
Pai_shun_hc.values.add ("0001", "Administrator ");
Pai_shun_hc.values.add ("0002", "Sales Manager ");
Save:
// The expires attribute is used to set the cookie life time, that is, to set how long the cookie can be kept. You can see in the forum that there are many options to save the information when you log on, for example, "save for one month" or "save for one year. Datetime and timespan are used to combine and generate a time period. For more information about the two, see. Net framewokr SDK documentation.
Datetime defaults _shun_dt = datetime. now;
Timespan performance_shun_ts = new timespan (0, 0, 2, 0 );
// Timespan constructor day, hour, minute, and second
Pai_shun_hc.expires = pai_shun_dt.add (pai_shun_ts );
// Add the instance to the httpresponse cookies set for future use
Usage:
// The following is a common method to use cookies. First, check whether there is a cookie. If there is a cookie, obtain the value. Otherwise, the value is ....
Simple Application of httpcookie