HttpCookie myCookie = new HttpCookie("MyCookie"); myCookie.Value = "Hello from the server!"; myCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(myCookie);
HttpCookie myCookie = Request.Cookies["MyCookie"]; if (myCookie != null) { string cookieValue = myCookie.Value; // do something with the cookie value }This code retrieves the "MyCookie" cookie from the request object's Cookies collection and checks if it exists. If it does, the code retrieves the value of the cookie into a string variable. The System.Web namespace is part of the .NET Framework Class Library, which is included with the .NET Framework.