How to write a cookie in C #

Source: Internet
Author: User

  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 #

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.