(Original) cookie Usage Details

Source: Internet
Author: User

This section describes the basic usage of cookies.

1. COOKIE principles

When the client accesses the server for the first time

When the client accesses the server again

2. Create a COOKIE

First, reference the System. Web namespace

 

Code

// Create a COOKIE object
HttpCookie myCookie = new HttpCookie ("UserInfo ");
// Store the COOKIE value
MyCookie ["UserName"] = "UserName ";
MyCookie ["UserSex"] = "UserSex"; // set the domain name myCookie related to this COOKIE. domain = "cnblogs.com"; // set the COOKIE validity period. expires = DateTime. now. addDays (5 );
// Add a COOKIE to the current web Request
Response. Cookie. Add (myCookie );

 

3. Read COOKIE

HttpCookie myCookie = Resquest. Cookie ["UserInfo"];
String userName;
If (myCookie! = Null)
{
UserName = myCookie ["UserName"];
}

 

4. Cancel Cookie

 

HttpCookie myCookie = new HttpCookie ("UserInfo ");
MyCookie. Expires = DateTime. Now. AddDays (-1 );
Response. Cookies. Add (myCookie );

 

5. How to view local cookies

First, set the hidden file to visible.

Open C:/Documents and Settings/current user/Cookies/to view the COOKIE file!

 

 

 

 

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.