Write protected void Button1_Click (object sender, EventArgs e) {HttpCookie cookie=new HttpCookie ("Mycook");//First-order and set the name of the cookie DateTime Dt=datetime.now; TimeSpan ts = new TimeSpan (0, 0, 1,0,0);//The Expiration time is 1 minute cookie. Expires = dt. ADD (TS);//Set expiration cookie. Values.add ("userid", "Userid_value"); Cookies. Values.add ("Userid2", "userid2_value2"); Response.appendcookie (cookie); Export all content of the cookie//response.write (cookie. Value);//output is: userid=userid_value&userid2=userid2_value2}//Read protected void Button2_Click (object sender, Eventa RGS e) {//HttpCookie Cokie = new HttpCookie ("Mycook");//initial Enable if (request.cookies["Mycook"]!=null) {// Response.Write ("The value of the key in the cookie is userid:" + request.cookies["Mycook" ["userid"]);//Whole line//response.write (" The value of the key value in the cookie is Userid2 "+ request.cookies[" Mycook "[" Userid2 "]); Response.Write (request.cookies["Mycook"). value);//Output All Values}}//modify cookie protected void Button3_Click (object sender, EventArgs e) {//Get client cookie object HttpCookie cok = request.cookies["Mycook"]; if (COK! = null) {//Modify the cookie in two ways, Cok. values["userid"] = "alter-value"; Cok. Values.set ("userid", "Alter-value"); Add new content to the cookie, COK. Values.set ("newid", "newvalue"); Response.appendcookie (COK); }}//delete cookie protected void Button4_Click (object sender, EventArgs e) {HttpCookie cok = request.cookies["MyCo OK "]; if (COK! = null) {if (! checkbox1.checked) {Cok. Values.remove ("userid");//Remove the value of the key value as UserID} else {TimeSpan ts = new TimeSpan (-1, 0, 0, 0); Cok. Expires = DATETIME.NOW.ADD (TS);//delete the entire cookie, as long as the expiration time is set to now} Response.appendcookie (COK); }} The analog login is HttpWebRequest HttpCookie Usercookie = new HttpCookie ("") Usercookie.value = ""; Usercookie.expires = DataTime.Now.AddMonth (1); HTTPCONTEXT.RESPONSE.COOKIES.ADD (Usercookie);
How to write a cookie in C #