Asp.net cookie operation

Source: Internet
Author: User

Asp tutorial. net cookie operations
Cookie limit to a folder or application

Httpcookie appcookie = new httpcookie ("appcookie ");
Appcookie. value = "written" + datetime. now. tostring ();
Appcookie. expires = datetime. now. adddays (1 );
Appcookie. path = "/application1 ";
Response. cookies. add (appcookie );

Clear cookie

Httpcookie cookie = request. cookies ["username"];
Cookie. expires = datetime. now. adddays (-30 );
Response. cookies. add (cookie );


Cookie operation

Response. cookies ["userinfo"] ["username"] = "patrick ";
Response. cookies ["userinfo"] ["lastvisit"] = datetime. now. tostring ();
Response. cookies ["userinfo"]. expires = datetime. now. adddays (1 );

Httpcookie acookie = new httpcookie ("userinfo ");
Acookie. values ["username"] = "patrick ";
Acookie. values ["lastvisit"] = datetime. now. tostring ();
Acookie. expires = datetime. now. adddays (1 );
Response. cookies. add (acookie );

Under normal conditions, all cookies of a site are stored on the client together, and all cookies are sent to the server together with any requests sent to the site. That is to say, each page of a website can obtain all the cookies of the site. However, you can set cookie parameters in two ways.


Restrict the cookie domain range

Response. cookies ["domain"]. value = datetime. now. tostring ();
Response. cookies ["domain"]. expires = datetime. now. adddays (1 );
Response. cookies ["domain"]. domain = "support.contoso.com ";


Read cookie
When a browser sends a request to the server, the cookie of the server is sent along with the request. In the asp.net tutorial application, you can use the httprequest object to read cookies. This object can be used as the request attribute of the page class. The structure of the httprequest object is basically the same as that of the httpresponse object. Therefore, you can read cookies from the httprequest object in the same way as writing cookies to the httpresponse object. The following code example demonstrates two methods. You can obtain the cookie value named username through these two methods and display the value in the label control:

If (request. cookies ["username"]! = Null)
Label1.text = server.html encode (request. cookies ["username"]. value );

If (request. cookies ["username"]! = Null)
{
Httpcookie acookie = request. cookies ["username"];
Label1.text = server.html encode (acookie. value );
}

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.