Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Sunday, February 12, 2012

Cookies in asp.net

Here we will discuss what is a cookie, as well as the types of cookies available in asp.net. You can also check Difference between cookie and session in asp.net.
- A cookie can be defined as small amount of memory used by the web server within the client system.

- The cookie are requested to maintain personal information of the client. Information like user name, no of visits, last visited date etc are get stored.

There are 2 types of cookies:
1. Inmemory cookie
2. Persistant cookie

Inmemory Cookie:
- When the cookie is maintained within browser process memory then it is called as inmemory cookie. This is very much temporary which will be erased when the browser is closed.

Create cookie:
 HttpCookie obj = new HttpCookie("nameofcookie");
 obj.Value = "100";
 Response.AppendCookie(obj); //Here cookie will be send to the browser.

Reading cookie:
 HttpCookie obj;
 obj = Request.Cookies["nameofcookie"]; //Returns the value from the cookie

Persistent cookie:
- When the browser maintains cookie on to harddisk memory then it is called as persistant cookie.
- This will have a life time, when the cookie will earse.

Create cookie:
HttpCookie obj = new HttpCookie("nameofcookie");
obj.Value = "100";
obj.Expires.AddDays(5);//Cookie will expire after 5 days
Response.AppendCookie(obj);

Reading cookie:
 HttpCookie obj;
 obj = Request.Cookies["nameofcookie"]; //Returns the value from the cookie

Check Interview Questions and Answers here.




0 on: "Cookies in asp.net"