Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class cookie:system.web.ui.page{protected void Page_Load (object sender, Eve Ntargs e) {}//create cookie protected void Button1_Click (object sender, EventArgs e) {HttpCookie Cooki E = new HttpCookie ("MyCookie"); DateTime Dt=datetime.now; TimeSpan ts=new TimeSpan (0,0,1,0,0);//expiry time set to 1 minutes//Set expiration time Cookie.expires = dt. ADD (TS); COOKIE.VALUES.ADD ("name", "Yys"); COOKIE.VALUES.ADD ("Age", "24"); Response.appendcookie (Cookie); }//Read protected void Button2_Click (object sender, EventArgs e) {if (request.cookies["MyCookie"]!=null) {Response.Write (request.cookies["MyCookie"). Value); }}//Modify protected void Button3_Click (object sender, EventArgs e) {//Get the cookie object of the client HttpCookie COK = ReQuest. cookies["MyCookie"]; if (cok!=null) {cok. values["name"] = "Abner"; Cok. Values.set ("Age", "20"); Add new content to the cookie, COK. Values.set ("Sex", "male"); Response.appendcookie (COK); Response.Write (request.cookies["MyCookie"). Value); }}//delete protected void Button4_Click (object sender, EventArgs e) {HttpCookie co=request.cookies["MyCo Okie "]; Co. Values.remove ("Sex");//Key-value pair Response.Write (request.cookies["MyCookie") with the key "sex" removed. Value); TimeSpan ts = new TimeSpan ( -1,0,0,0); Co. Expires = DATETIME.NOW.ADD (TS);//delete the entire cookie, just set the expiration time to now}}
Use of cookies