You might have heard about cookies, but what exactly are they and what we can do with them? we will learn that in this tutorial.
Cookies are one of the state management techniques,These are nothing but small files that are created and stored on the client's system or on client's browser memory.
If a web application uses cookies, the server sends cookies and the client browser will store it. the browser then returns the cookie to the server the next time page is requested.
The advantages for cookies are, easy to implement for multiple sites, client browser will take care of them.
Main disadvantages are storing the data in simple text format which is will be transperent to the user, size limit.
//How to create Cookie.
HttpCookie userCookies = newHttpCookie("SampleCookie");
userCookies["UserName"] = "Joe";
userCookies["Location"] = "NY";
Response.Cookies.Add(userCookies);
// Reading data from cookies.
HttpCookie userCookies = Request.Cookies["SampleCookie"];
String ULocation;
ULocation = userCookies["Location"];
Deleting cookies:
If you want to remove cookies, the only way is to replace the existing cookie with a past expiration date.
HttpCookie userCookies = newHttpCookie("SampleCookie");
userCookies.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(userCookies);
They are two types of cookies, Persistent cookies and Non persistent cookies
Persistent cookies are stored in the client machine until they expire and they are generally used to collect identification about user from the system, where as Non-persistent cookies are temporary cookies and are stored in browsers memory.