Loading ...

How we create,Read,Delete Cookies | CodeAsp.Net

How do we create,Read,Delete Cookies in Asp.net

 /5
0 (0votes)

A cookie is a small bit of text file that browser creates and stores on your machine (hard drive). Cookie is a small piece of information stored as a string. Web server sends the cookie and browser stores it, next time server returns that cookie.Cookies are mostly used to store the information about the user. Cookies are stores on the client side.
How to create the cookies:

 

//for store the values in the cookies
Response.Cookies["MyName"].Value = "vijendra";
//how much time cookies save on the user hard drive
Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(10);
// now this cookies save 10 days on the user hard drive.after 10 days this cookies expires.

if  we do not add   Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(10); and  we create the cookie in that case cookie will not  store on the user hard drive, then it will  stores on the browser, when we close the browser the cookie automatically Expires.
How to Read the cookie:

When we want to read the cookies then first we check the cookie whether  exit or not:

 

string myName=string.Empty;
if(!string.IsNullOrEmpty(Request.Cookies["MyName"]))
{
// assign cookie value in the variable.
myName = Request.Cookies["MyName"].Value;
}


How to Delete the cookies:

First we check cookie exit or not:

  

  if(!string.IsNullOrEmpty(Request.Cookies["MyName"]))
    {
     // Set its Expirey time in the past
    Response.Cookies["MyName"].Expires = DateTime.Now.AddDays(-1);
    }

Comments (2)

   
Pooja
Pooja
Very nice Description. I was looking for the physical path of cookie and didnt get it anywhere buut got here... Thanks
8/11/2010
 · 
by
   
Ashish Pandey
Ashish Pandey
This is one of the best articles so far I have read online. Just useful information. Very well presented. Thanks for sharing with us. Check out this link too...
http://mindstick.com/Articles/c4e253f5-67dc-4211-8a1f-e29c17ce2943/?Cookies%20in%20ASP.NET

Its also helped me lot!!
1/12/2012
 · 
by
  • :*
  • :*
  • :
 *

Top Posts