Asp. NET read and write cookie Data sample code

Source: Internet
Author: User
Tags datetime

ASP. NET Cookies: Create Cookie Method (1)


response.cookies["UserName"].  Value = "admin";


response.cookies["UserName"].  Expires = DateTime.Now.AddDays (1);


//If no expiration time is set, the cookie information will not be written to the user's hard drive and the browser shutdown will be discarded.





ASP. NET Cookies: Create Cookie Method (2)


HttpCookie acookie = new HttpCookie ("lastvisit");


//Last access time


Acookie.value = DateTime.Now.ToString ();


acookie.expires = DateTime.Now.AddDays (1);


Response.Cookies.Add (Acookie);





ASP. NET Cookies: Access Cookie Method (1)


if (request.cookies["userName"]!= null)


Label1.Text = Server.HTMLEncode (request.cookies["UserName").   Value);





Access Cookie Method (2)


if (request.cookies["userName"]!= null) {


HttpCookie acookie = request.cookies["UserName"];


Label1.Text = Server.HTMLEncode (Acookie.value);


}  





ASP. NET cookies: Creating a multivalued cookie method (1)
response.cookies["UserInfo" ["userName"] = "admin";


response.cookies["UserInfo" ["lastvisit"] = DateTime.Now.ToString ();


response.cookies["UserInfo"].   Expires = DateTime.Now.AddDays (1);





ASP. NET cookies: Creating a multivalued Cookie method (2)
HttpCookie acookie = new HttpCookie ("UserInfo");


acookie.values["userName"] = "admin";


acookie.values["lastvisit"] = DateTime.Now.ToString ();


acookie.expires = DateTime.Now.AddDays (1);


Response.Cookies.Add (Acookie);





ASP. NET cookies: Read multivalued cookies


HttpCookie acookie = request.cookies["UserInfo"];


string username=acookie.values["UserName"];


string lastvisit=acookie.values["lastvisit"];





ASP. NET cookies: modifying and deleting cookies
cannot modify or delete cookies directly, only a new cookie can be created and sent to the client to implement modify or delete cookies.

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.