Operation of Cookies in C #. Net

Source: Internet
Author: User

One, add cookies
1 HttpCookie cookie = new HttpCookie ("Info"); Defines a cookie object and an item named info
2 DateTime dt = DateTime.Now; Defining a Time Object
3 TimeSpan ts=new TimeSpan (1,0,0,0); Cookie effective time, specifically check MSDN
4 cookies. Expires = dt.                 ADD (TS); Add Action Time
5 cookies.          Values.add ("User", "CXBKKK"); Add properties
6 cookies. Values.add ("userid", "1203");
7 Response.appendcookie (cookie); OK write in Cookie

Second, read cookies
1 if (request.cookies["Info"]!=null)/Read cookie

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}
Or else
13 {
Response.Write ("error");
15}

Third, modify cookies
1 response.cookies["Info" ["user"] = "2"; modifying cookies
2 response.cookies["Info"]. Expires = DateTime.Now.AddDays (1);

Four, delete the property under the cookie
1 HttpCookie acookie=request.cookies["Info"];
2 Acookie.                 Values.remove ("userid"); Delete a property under a cookie
3 Acookie. Expires = DateTime.Now.AddDays (1);
4 Response.Cookies.Add (Acookie);

Five, delete all cookies, that is, set the expiration time is now the line
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}

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.