C # cookies

Source: Internet
Author: User

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.

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.