Simple Application of httpcookie

Source: Internet
Author: User
Tags first string

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

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.