asp.net various cookie codes and parsing instances

Source: Internet
Author: User
Tags datetime

  This article mainly introduces the ASP.net various cookie code and the analytic instance, needs the friend to be possible to refer to under

A cookie is a piece of textual information in which the client store cookie is one of the methods that ASP.net session state will request to be associated. 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 only no more than 4096 bytes can be guaranteed to be accepted.   Write cookies     code as follows://mode 1:response.cookies["username"].value= "Mike"; response.cookies["username"]. expires=datetime.maxvalue;   //mode 2:httpcookie Acookie = new HttpCookie ("last"); Acookie. Value= "a"; Acookie.. expires=datetime.maxvalue;  Response.Cookies.Add (Acookie);    /mode 1:response.cookies["Userinfo1" ["Name"].value= "Mike"; response.cookies["Userinfo1" ["Last"].value= "a"; response.cookies["Userinfo1"]. expires=datetime.maxvalue;   //mode 2:httpcookie cookie = new HttpCookie ("Userinfo1"); Cookie. values["Name"]= "Mike"; Cookie. values["Last"]= "a"; Cookie. expires=datetime.maxvalue; //cookie. Expires = System.DateTime.Now.AddDays (1);//Set Expiration Time  1 day Response.Cookies.Add (cookie);       Read cookie  Internet Explorer saves a site Cookie in the file name format of <USER>@<DOMAIN>.TXIn the file of T, where <user> is your account name. Note: You should ensure that the cookie does exist before you get the value of the cookie. Otherwise, you will get an exception   code as follows: if (request.cookies["UserName"]!=null) {  string str = Request.Cookies ("UserName"). Read If value; }  //multivalued cookies (request.cookies["UserInfo1"]!=null) {  string name=request.cookies["Useri" Nfo1 "] [" name "];   String last=request.cookies["UserInfo1" ["Last"]; }    //Read Cookie collection for (int i = 0; i<request.) Cookies.count i++) {    HttpCookie cookies = request.cookies     Response.Write ("Name=" +cookies. Mame+ "<br/>");     if (cookies). HasKeys)//Is there a subkey     {        System.Collections.Specialized.NameValueCollection Namecoll                                   &NB Sp            = acookie.values;         for (int j=0;j<namecoll.count;j++)       &NBSP {            Response.Write ("subkey name =" + Namecoll.allkey[j] + "<br/>");     &NBSP ;       Response.Write ("subkey value =" + Namecoll[j] + "<br/>");        }       {    else     {        Respon Se. Write ("value=" +cookies. Value+ "<br/>");            }}       When you run this code, you see a cookie,asp named "Asp.net_sessionid". NET uses this Cookie to save a unique identifier for your session.   Modify the cookie  modification method is the same as the creation method   Delete cookie  to set its validity period to a date in the past. When the browser checks the validity of a cookie, it deletes the expired cookie.     Code as follows: HttpCookie cookie = new HttpCookie ("Userinfo1"); Cookie. Expires=datetime.now.adddays ( -30);  Response.Cookies.Add (cookie);     Modify Cookie  response.cookies["Info" ["user"] = "2";  response.cookies["Info"]. Expires = DateTime.Now.AddDays (1);        //Delete cookies under properties      httpcookie 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 on the line      int limit=request.cookies.count-1;  for (int i=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 cookies to share, you should add the following settings     code as follows: Cookies. Domain = ". primary domain name";//For example. keleyi.com cookies. Path = "/";   Cookie.expires AddDays (-1) is an immediate expiration    
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.