Added, modified, and deleted cookies for ASP. NET operations

Source: Internet
Author: User

Cookies, sometimes in the form of cookies, are the data (usually encrypted) stored on the user's local terminal by some websites to identify the user ). Defined in rfc2109. It was invented in March 1993 by Lou montulli, a former employee of Netscape.

The server can filter and regularly maintain the information contained in cookies to determine the status during HTTP transmission. The most typical application of cookies is to determine whether a registered user has logged on to the website. The user may be prompted to determine whether to keep the user information during the next visit to simplify the logon procedures, these are the functions of cookies. Another important application scenario is "Shopping Cart. Users may select different items on different pages of the same website within a period of time. These information will be written into cookies so that information can be extracted during the final payment.

Cookie can maintain the login information to the user's next session with the server. In other words, when accessing the same website next time, the user will find that the user has logged on without having to enter the user name and password (of course, users cannot delete cookies manually ). Some cookies are deleted when the user exits the session, which can effectively protect personal privacy.

 

Program Code // Write
Protected void button#click (Object sender, eventargs E)
{
Httpcookie cookie = new httpcookie ("mycook"); // initialize and set the cookie name
Datetime dt = datetime. now;
Timespan Ts = new timespan (0, 0, 1, 0, 0); // The expiration time is 1 minute.
Cookie. expires = DT. Add (TS); // set the expiration time
Cookie. Values. Add ("userid", "userid_value ");
Cookie. Values. Add ("userid2", "userid2_value2 ");
Response. appendcookie (cookie );
// Output all content of the cookie
// Response. Write (cookie. Value); // The output is: userid = userid_value & userid2 = userid2_value2
}

// Read
Protected void button2_click (Object sender, eventargs E)
{

// Httpcookie Cokie = new httpcookie ("mycook"); // initialize
If (request. Cookies ["mycook"]! = NULL)
{
// Response. Write ("the key value in the cookie is userid:" + request. Cookies ["mycook"] ["userid"]); // The whole line
// Response. Write ("the key value in the cookie is userid2" + request. Cookies ["mycook"] ["userid2"]);
Response. Write (request. Cookies ["mycook"]. Value); // output all values
}
}

// Modify the cookie
Protected void button3_click (Object sender, eventargs E)
{
// Obtain the cookie object of the Client
Httpcookie COK = request. Cookies ["mycook"];

If (COK! = NULL)
{
// Two methods for Cookie Modification
COK. Values ["userid"] = "alter-value ";
COK. Values. Set ("userid", "alter-value ");

// Add new content to the cookie
COK. Values. Set ("newid", "newvalue ");
Response. appendcookie (COK );
}
}

// Delete the cookie
Protected void button4_click (Object sender, eventargs E)
{

Httpcookie COK = request. Cookies ["mycook"];
If (COK! = NULL)
{
If (! Checkbox1.checked)
{
COK. Values. Remove ("userid"); // remove the value whose key value is userid
}
Else
{
Timespan Ts = new timespan (-1, 0, 0, 0 );
COK. expires = datetime. Now. Add (TS); // Delete the entire cookie, as long as the expiration time is set to the current
}
Response. appendcookie (COK );
}
}

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.