Protected Void Button2_click ( Object Sender, eventargs E)
{
Httpcookie = 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 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 ); // Output: userid = userid_value & userid2 = userid2_value2
}
//Read
Protected VoidButton#click (ObjectSender, eventargs E)
{
// Httpcookie Cokie = new httpcookie ("mycook "); // Initial tutorial
If (Request. Cookies [ " Mycook " ] ! = Null )
{
// Response. Write ("the key value in the cookie is userid:" + request. Cookies ["mycook"] ["userid"]); // 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 cookie
Protected Void Button3_click ( Object Sender, eventargs E)
{
// Obtain the cookie object of the client.
Httpcookie COK = Request. Cookies [ " Mycook " ];
If (COK ! = Null )
{
// Cookie modification methods
COK. Values [ " Userid " ] = " Alter-Value " ;
COK. Values. Set ( " Userid " , " Alter-Value " );
//Add new content to cookies
COK. Values. Set ("Newid","Newvalue");
Response. appendcookie (COK );
}
}
//Delete cookie
Protected VoidButton4_click (ObjectSender, eventargs E)
{
Httpcookie COK = Request. Cookies [ " Mycook " ];
If (COK ! = Null )
{
If ( ! Checkbox1.checked)
{
COK. Values. Remove ( " Userid " ); // Remove the userid key value.
}
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 );
}
}
Previously written Program Because ASP. net saves data on the client in many ways, and previously written programs do not use cookies, so I did not understand it. Today, a program always uses cookies for errors. Common Methods for checking cookies on the Internet.
Cookie usage is similar to ASP. For example, we create a cookie named user with a value of 111.
Httpcookie = New Httpcookie ( " User " );
Cookie. Value = " 111 " ;
Response. appendcookie (cookie );
It's easy to retrieve the cookie value.
Httpcookie = Request. Cookies [ " User " ];
Cookievalue = Cookie. value;
Sometimes we want to store multiple pieces of information in a cookie. For example, we add multiple information under the cookie named user.
Httpcookie = New Httpcookie ( " User " );
Cookie. Values. Add ( " Name " , " 222 " );
Cookie. Values. Add ( " PWD " , " 333 " );
Cookie. Values. Add ( " Member " , " Admin " );
Response. appendcookie (cookie );
Retrieving information from a client is also simple.
Httpcookie = Request. Cookies [ " User " ];
Value1 = Cookies. Values [ " Name " ];
Value2 = Cookies. Values [ " PWD " ];
Method 2,
Write:
Code As follows:
Httpcookie = New Httpcookie ( " Id_admin _ " );
Cookie. Value = Model. id_admin _. tostring ();
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );
Cookie = New Httpcookie ( " Name_admin _ " );
// It may be a Chinese character and must be encoded
Cookie. Value = Httputility. urlencode (model. name_admin _);
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );
Cookie = New Httpcookie ( " Guid " );
Cookie. Value = Guid. newguid (). tostring ();
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );
Read:
The Code is as follows:
Httpcontext. Current. Request. Cookies ["Guid"]. Value
Asp.net clear cookie clear a single
The Code is as follows:
Response. Cookies ["Admin"]. Expires=Datetime. Now. adddays (-1);
Asp.net clear cookie clear all
request. cookies. the clear () method does not delete a cookie.
deleting a cookie (that is, physically removing a cookie from a user's hard disk) is a form of cookie modification.
the cookie cannot be directly removed because it is on the user's computer.
however, the browser can delete cookies for you.
This technique creates a new cookie with the same name as the cookie to be deleted.
set the cookie expiration date to a date earlier than the current date.
when the browser checks the cookie expiration date, the browser discards the expired cookie.
the following code example shows how to delete all available cookies in an application:
code:
httpcookie acookie;
string cookiename;
int limit = request. cookies. count;
for ( int I = 0 ; I limit; I ++ )
{< br> cookiename = request. cookies [I]. name;
acookie = New httpcookie (cookiename );
acookie. expires = datetime. now. adddays ( - 1 );
response. cookies. add (acookie);
}