Summary of cookie usage in C #

Source: Internet
Author: User
When using cookies today, you may encounter some problems:
One is that the cookie always displays garbled characters when reading the content: I have never understood why it is like this. Later I want to use the server. urlencode encodes the content and decodes it when it is displayed:

The following is a simple cookie written by myself.
CompleteCodeAs follows:
Protected void button2_click (Object sender, eventargs E)
{
String cookiename = "123 ";
Httpcookie cookie = new httpcookie (cookiename );
Cookie. value = server. urlencode ("Han Guizhou ");
Datetime dtnow = datetime. now;
Timespan tsminute = new timespan (0, 1, 0, 0 );
Cookie. expires = dtnow + tsminute;
Response. Cookies. Add (cookie );
Label1.text = cookie. value;
}
Protected void button#click (Object sender, eventargs E)
{
Httpcookie getcookie = request. Cookies ["123"];
If (Null = getcookie)
{
Response. Write ("corresponding cookie not found ");
}
Else
{
Label1.text = server. urldecode (getcookie. value. tostring ());

}
}

In addition, it should be noted that the method to determine whether the cookie content exists is
If (null = cookie name)

Third, pay attention to the cookie survival time:
In this example. Its survival time is one hour.
Datetime dtnow = datetime. now;
Timespan tsminute = new timespan (0, 1, 0, 0 );
Cookie. expires = dtnow + tsminute;

Related Article

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.