Access code for cookies in asp tutorial. net c #
As long as no expiration time is set for the cookie, the cookie will automatically expire when the browser is closed.
Cookie creation:
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.html encode (request. cookies ["username"]. value ));
}
Read with subkeys:
If (request. cookies ["user"]! = Null)
{
Response.write(server.html encode (request. cookies ["user"] ["username"]. value ));
Response.write(server.html encode (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.html controls;
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.html controls;
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