Create a username cookie on the client. Its value is gjy and its validity period is one day.
Method 1:
Response. Cookies ["username"]. value = "zxf ";
Response. Cookies ["username"]. expires = datetime. Now. adddays (1 );
Method 2:
System. Web. httpcookie newcookie = new httpcookie ("username ");
Newcookie. value = "gjy ";
Newcookie. expires = datetime. Now. adddays (1 );
Response. appendcookie (newcookie );
Create cookies with subkeys:
System. Web. httpcookie newcookie = new httpcookie ("user ");
Newcookie. Values ["username"] = "zxf ";
Newcookie. Values ["password"] = "111 ";
Newcookie. expires = datetime. Now. adddays (1 );
Response. appendcookie (newcookie );
Read cookies:
No sub-key read:
If (request. Cookies ["username"]! = NULL)
{
Response. Write (server. htmlencode (request. Cookies ["username"]. Value ));
}
Read with subkeys:
If (request. Cookies ["user"]! = NULL)
{
Response. Write (server. htmlencode (request. Cookies ["user"] ["username"]. Value ));
Response. Write (server. htmlencode (request. Cookies ["user"] ["password"]. Value ));
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public class cookie
{
/// <Summary>
/// Cookies assignment
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Param name = "strvalue"> key value </param>
/// <Param name = "strday"> valid days </param>
/// <Returns> </returns>
Public bool setcookie (string strname, string strvalue, int strday)
{
Try
{
Httpcookie cookie = new httpcookie (strname );
// Cookie. Domain = ".xxx.com"; // when cross-domain access is required, you can specify a domain name for the cookie in the format of .xxx.com
Cookie. expires = datetime. Now. adddays (strday );
Cookie. value = strvalue;
System. Web. httpcontext. Current. response. Cookies. Add (cookie );
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Read cookies
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Returns> </returns>
Public String getcookie (string strname)
{
Httpcookie cookie = system. Web. httpcontext. Current. Request. Cookies [strname];
If (cookie! = NULL)
{
Return cookie. value. tostring ();
}
Else
{
Return NULL;
}
}
/// <Summary>
/// Delete cookies
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Returns> </returns>
Public bool delcookie (string strname)
{
Try
{
Httpcookie cookie = new httpcookie (strname );
// Cookie. Domain = ".xxx.com"; // when cross-domain access is required, you can specify a domain name for the cookie in the format of .xxx.com
Cookie. expires = datetime. Now. adddays (-1 );
System. Web. httpcontext. Current. response. Cookies. Add (cookie );
Return true;
}
Catch
{
Return false;
}
}
}
Example:
Cookie = new cookie ();
Cookie. setcookie ("name", "AAA", 1); // value assignment
Cookie. getcookie ("name"); // Value
Cookie. delcookie ("name"); // Delete
Note: When the cookie contains Chinese characters with garbled characters, it is encoded with Chinese characters, such as cookies. setcookie ("name", server. urlencode ("AAA"), 1), decoded upon reading
In addition, as long as no expiration time is set for the cookie, the cookie will automatically expire when the browser is disabled.
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public class cookie
{
/// <Summary>
/// Cookies assignment
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Param name = "strvalue"> key value </param>
/// <Param name = "strday"> valid days </param>
/// <Returns> </returns>
Public bool setcookie (string strname, string strvalue, int strday)
{
Try
{
Httpcookie cookie = new httpcookie (strname );
// Cookie. Domain = ".xxx.com"; // when cross-domain access is required, you can specify a domain name for the cookie in the format of .xxx.com
Cookie. expires = datetime. Now. adddays (strday );
Cookie. value = strvalue;
System. Web. httpcontext. Current. response. Cookies. Add (cookie );
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Read cookies
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Returns> </returns>
Public String getcookie (string strname)
{
Httpcookie cookie = system. Web. httpcontext. Current. Request. Cookies [strname];
If (cookie! = NULL)
{
Return cookie. value. tostring ();
}
Else
{
Return NULL;
}
}
/// <Summary>
/// Delete cookies
/// </Summary>
/// <Param name = "strname"> Primary Key </param>
/// <Returns> </returns>
Public bool delcookie (string strname)
{
Try
{
Httpcookie cookie = new httpcookie (strname );
// Cookie. Domain = ".xxx.com"; // when cross-domain access is required, you can specify a domain name for the cookie in the format of .xxx.com
Cookie. expires = datetime. Now. adddays (-1 );
System. Web. httpcontext. Current. response. Cookies. Add (cookie );
Return true;
}
Catch
{
Return false;
}
}
}
Example:
Cookie = new cookie ();
Cookie. setcookie ("name", "AAA", 1); // value assignment
Cookie. getcookie ("name"); // Value
Cookie. delcookie ("name"); // Delete
Note: When the cookie contains Chinese characters with garbled characters, it is encoded with Chinese characters, such as cookies. setcookie ("name", server. urlencode ("AAA"), 1), decoded upon reading
In addition, as long as no expiration time is set for the cookie, the cookie will automatically expire when the browser is disabled.