Programming> ASP. NET 3.5 session control> cookie

Source: Internet
Author: User
Tags allkeys

Transferred from: http://www.cnblogs.com/fooo/archive/2008/01/07/1029119.html

// ------------------------------- Load

If (! Ispostback)
{

Httpcookie cookie = request. Cookies ["userinfo1"];
Cookie. expires = system. datetime. Now. addminutes (20); // set the expiration time
For (INT I = 0; I <5; I ++)
{
Cookie. Values ["BB" + I. tostring ()] = I. tostring ();
Response. Cookies. Add (cookie );
}

}

// *************** Write
Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. expires = system. datetime. Now. addminutes (20); // set the expiration time
For (INT I = 0; I <10; I ++)
{

Cookie. Values ["U" + I. tostring ()] = I. tostring ();
Response. Cookies. Add (cookie );
}

// ************** ------------- Read
// Read the cookie set
For (INT I = 0; I <request. Cookies. Count; I ++)
{
If (request. Cookies. allkeys [I] = "userinfo1 ")
{

Httpcookie cookies = request. Cookies ["userinfo1"];
Response. Write ("name =" + cookies. Name + "<br/> ");
// Response. Write ("name =" + cookies. Value + "<br/> ");

If (cookies. haskeys) // whether a subkey exists
{
System. Collections. Specialized. namevaluecollection namecoll = cookies. values;
For (Int J = 0; j <namecoll. Count; j ++)
{

Response. Write ("subkey name =" + namecoll. allkeys [J] + "<br/> ");
Response. Write ("sub-key value =" + namecoll [J] + "<br/> ");
}

}
Else
{
Response. Write ("value =" + cookies. Value + "<br/> ");
}
}

}

// ************************ Delete
Httpcookie acookie = request. Cookies ["userinfo1"];
Acookie. expires = system. datetime. Now. addminutes (20); // set the expiration time
If (acookie. haskeys) // whether the subkey exists
{
System. Collections. Specialized. namevaluecollection namecoll = acookie. values;
For (Int J = 0; j <namecoll. Count; j ++)
{
If (namecoll. allkeys [J] = "u8 ")
{
Acookie. Values. Remove (namecoll. allkeys [J]);
Response. Cookies. Add (acookie );
}
}
}

// **************************** Append
Httpcookie cookie = request. Cookies ["userinfo1"];
Cookie. expires = system. datetime. Now. addminutes (20); // set the expiration time
For (INT I = 0; I <5; I ++)
{
Cookie. Values ["K" + I. tostring ()] = I. tostring ();
Response. Cookies. Add (cookie );
}

 

---------

Protected void button2_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 button#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 );
}
}

-------------------------------------

What is Cookie?

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.