About the operation of cookies in ASP.net C #

Source: Internet
Author: User
Asp.net|cookie today have time to the operation of the cookie write, although very simple, but to avoid the time to forget, before is forgotten still very experiment a little before get out, depressed.


below is the write cookie


1 HttpCookie cookie = new HttpCookie ("info");//Defines the cookie object and the item named info


2 DateTime dt = datetime.now;//Define Time Object


3 TimeSpan ts=new TimeSpan (1,0,0,0)//cookie effective action time, specific check MSDN


4 cookies. Expires = dt. Add (ts);//adding Action time


5 cookies. Values.add ("User", "CXBKKK")//Add attribute


6 cookies. Values.add ("userid", "1203");


7 Response.appendcookie (cookie);//OK to read cookies in write Cookie


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


2 {


3 string temp=convert.tostring (request.cookies["Info"). values["user") + "" +convert.tostring (request.cookies["Info"). values["userid"]);


4//Read all with request.cookies["Info"]. Value)


5 if (temp== "")


6 {


7 Response.Write ("Empty");


8}


9 Else


Response.Write (temp);


11}


Else


13 {


Response.Write ("error");


15} Modify Cookie


1 response.cookies["Info" ["user"] = "2";


2 response.cookies["Info"]. Expires = DateTime.Now.AddDays (1); Delete a property under a cookie


1 HttpCookie acookie=request.cookies["Info"];


2 acookie. Values.remove ("userid");


3 Acookie. Expires = DateTime.Now.AddDays (1);


4 Response.Cookies.Add (Acookie); Delete all cookies, that is, set the expiration time for now.


1 int limit=request.cookies.count-1;


2 for (int i=0;i<limit;i++)


3 {


4 Acookie = Request.Cookies (i)


5 acookie. Expires = DateTime.Now.AddDays (-1)


6 Response.Cookies.Add (acookie)


7} There's no need to look around





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.