What are Cookies?
Cookies are nothing but a small pieces of information that are sent by the server and stored in the client's browser. This small piece of information about the user is then sent back by the browser in all subsequent requests to the same URL in the request message.
We can store anything in a cookie; a general example of cookie usage is to store the user preferences, password remembering, storing user options and so on.
Generally, cookies are stored in plain text files in the local disk of the user. Cookies can be accessed from anywhere in the application. Generally cookies are lost after the user closes the browser but we can also have cookies that will persist even after the browser is closed.
Cookies are mainly classified in the following two types:
- persistent
- non-persistant.
Persistent cookies
As the name itself suggests, these cookies remain persistant in the client's memory even after the browser is closed. They remain permanently in memory until they are explicitly removed or their expiration is reached.
Non-persistent cookies
These cookies do not remain in the client's memory and are lost after the browser is closed.
How to create cookies from code
Generally we use the HttpCookie class to create an instance of a cookie for that session, then add the values that we want to be included in the cookie as key-value pairs. The following code explains this more clearly.
- HttpCookie userCookie = new HttpCookie("infoCookie");
- userCookie["username"] = "Amogh";
- userCookie["City"] = "Hyderabad";
- userCookie["Country"] = "India";
- //adding the cookie to the Response object. This will be sent to the client on first request from the client.
- Response.Cookies.Add(userCookie);

SubashPosted Sep 15, 2016, 1:21 AM
Nice info