Asp.net,cookie, write cookies, take cookies

Source: Internet
Author: User

A cookie is a piece of text information in which the client store Cookie is one of the methods that the session state of ASP. NET will request to associate with session. Cookies can also be used directly to persist data between requests, but the data is then stored on the client and sent to the server along with each request. The browser has a limit on the size of the Cookie, so it is guaranteed to be accepted only if it is not more than 4096 bytes.

Writing cookies

//Mode 1:response.cookies["username"].value="Mike"; response.cookies["username"]. expires=DateTime.MaxValue;//Mode 2:HttpCookie Acookie =NewHttpCookie (" Last"); Acookie. Value="a"; Acookie. Expires=DateTime.MaxValue; RESPONSE.COOKIES.ADD (acookie);//the notation of multi-valued cookies//Mode 1:response.cookies["Userinfo1"]["name"].value="Mike"; response.cookies["Userinfo1"][" Last"].value="a"; response.cookies["Userinfo1"]. expires=DateTime.MaxValue;//Mode 2:HttpCookie cookie =NewHttpCookie ("Userinfo1"); cookies. values["name"]="Mike"; a cookie. values[" Last"]="a"; a cookie. Expires=DateTime.MaxValue;//cookies. Expires = System.DateTime.Now.AddDays (1);//set Expiration time 1 daysRESPONSE.COOKIES.ADD (cookie);

Read cookies
Internet Explorer saves a site's Cookie in a file with a filename format of <user>@<domain>.txt, where <user> is your account name.
Note: Before you obtain the value of a cookie, you should ensure that the cookie does exist. Otherwise, you will get an exception



If (request.cookies["UserName"]!=NULL){  stringstr = Request.Cookies ("UserName"). Value; }//read a multi-valued cookieIf (request.cookies["UserInfo1"]!=NULL ){  stringname=request.cookies["UserInfo1"]["name"]; stringlast=request.cookies["UserInfo1"][" Last"]; }//Read the Cookie collection for(inti =0; i<request.cookies.count; i++.) {HttpCookie cookies=Request.Cookies; Response.Write ("name="+cookies. mame+"<br/>"); if(cookies.) HasKeys)//whether there are children keys{System.Collections.Specialized.NameValueCollection Namecoll =acookie.values;  for(intj=0; j<namecoll.count;j++) {Response.Write ("sub-Key name ="+ Namecoll.allkey[j] +"<br/>"); Response.Write ("sub-key value ="+ Namecoll[j] +"<br/>"); }    }    Else{Response.Write ("value="+cookies. Value+"<br/>"); }}


When you run this code, you see a cookie,asp named "Asp.net_sessionid". NET uses this Cookie to hold a unique identifier for your session.

Modify Cookies
The method of modification is the same as the creation method

Delete Cookies
Set its validity period to a date in the past. This expired cookie is deleted when the browser checks the validity period of the cookie.

New HttpCookie ("userinfo1"); cookies. Expires=datetime.now.adddays (-); RESPONSE.COOKIES.ADD (cookie);


Modify Cookies

response.cookies["Info"]["User"] ="2"; response.cookies["Info"]. Expires = DateTime.Now.AddDays (1);//Delete a property under a cookieHttpCookie acookie=request.cookies["Info"]; Acookie. Values.remove ("userid"); Acookie. Expires= DateTime.Now.AddDays (1);  RESPONSE.COOKIES.ADD (Acookie); //Delete all cookies, that is, set the expiration time for now. intLimit=request.cookies.count-1;  for(intI=0; i<limit;i++) {Acookie=request.cookies (i) acookie. Expires= DateTime.Now.AddDays (-1) Response.Cookies.Add (acookie)}

-------------

If you have a master station and a level two domain name station and the cookie is shared, add the following settings

Cookies. Domain = ". primary domain";
Cookies. Path = "/";

Asp.net,cookie, write cookies, take 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.