Saturday 2 June 2012

Creating a Cookie


<b> To create a cookie</b>, you can use Response.Cookies. Eg : -
Code:
Response.Cookies["Name"].Value = "Vijay";
Response.Cookies["Name"].Expires = DateTime.Now.AddDays(1);


In here, the first line will create a cookie with the name "Name" in client machine with value "Vijay".The following line states that the cookie will expire in one day.


<b>How to retrieve values from Cookies ?</b>

To read values from cookies, use the Request object. You must have created cookies to do so.
Code:
if (Request.Cookies["Name"]!=null) //check if cookie exists
{
   string name = Response.Cookies["Name"].value; //returns string format
}

No comments:

Post a Comment