Friday, April 24, 2009

Cookies in asp.net

Cookies are one of the way to store data during the time when web server and browser are not
connected. common use of cookies is to remember users between visits.Cookies is a small text file
sent by web server and saved by web browser on client machine

:!: Creating cookies in asp.net

using system

//saving cookie
Response.Cookies["Cookiename"].Value="biscuit";

//when cookiesz expires
Response.Cookies["Cookiename"].Expires = DateTime.Now.AddDays(1);



:!: Reading cookies in asp.net

string CookienameValue;

// if cookie not exists
if(Request.Cookies["Cookiename"] != null)
CookienameValue = Request.Cookies["Cookiename"].Value;


:!: Deleting Cookies in asp.net

// First check if cookie exists
if (Request.Cookies["Cookiename"] != null)
{
// Set its expiration time somewhere in the past
Response.Cookies["Cookiename"].Expires = DateTime.Now.AddDays(-1);

}