How the Web uses COOKIE,MVC to use cookies

Source: Internet
Author: User

1. There are two ways to create cookies:

response.cookies["userName"Patrick"; response.cookies["userName"]. Expires = DateTime.Now.AddDays (1);         
New HttpCookie ("lastvisit"); acookie.value = DateTime.Now.ToString (); acookie.expires = DateTime.Now.AddDays (1); RESPONSE.COOKIES.ADD (Acookie);      

2. Read cookies

if (request.cookies[ "username"]! = null "username" ]. Value); if (Request.cookies[ Username "]! = null) { HttpCookie acookie = Request.cookies[ "username ""; Label1.Text = Server.HTMLEncode (acookie.value);}       

3. Modification and deletion of cookies

IntCounterif (request.cookies["Counter "] = = null) counter = Span style= "color: #800080;" >0; Else{counter = int. Parse (Request.cookies[ "counter  "". Value);} Counter++ "counter  "]. Value = counter. ToString (); Response.cookies[ "counter  "]. Expires = DateTime.Now.AddDays (1);         

Be sure to note that setting cookies is read with Response is not the same with Request !

Delete Cookies

String"userName"; HttpCookie acookie = request.cookies["userInfo"];acookie.values.remove (SubkeyName); Acookie.expires = DateTime.Now.AddDays (1); RESPONSE.COOKIES.ADD (Acookie);         

HttpCookie Acookie; string cookiename;  int limit = Request.Cookies.Count;  for (int i=0; i<limit; i++) {cookiename =new HttpCookie (cookiename); acookie.expires = DateTime.Now.AddDays (-1); RESPONSE.COOKIES.ADD (Acookie);}           

How the Web uses COOKIE,MVC to use cookies

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.