Cookie Access Code in asp.net C #

Source: Internet
Author: User
Tags html encode setcookie

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

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.