Monday, June 28, 2010

ASP .NET Cookies

Introduction :
When user visit a website for the first time a .txt file sent to users web browser and stored in a user system in a particular location this text file termed as cookies.
What is Cookie?
When user visits a website for the first time a .txt file is sent to user's web browser and stored in a user system in a particular location, This text file is termed as cookie.
Use of cookie?
The main purpose of cookies is to know the user who visited websites. For instance, you can also save a small piece of information inside cookies.
You might have seen almost every website login page having one checkbox "Remember Me on this computer". These websites store the login information like username and password inside the cookies. So, next time when user logs in, the website retrieves the information from cookies and you will be logged in automatically.
Types of cookies:
1) Session Cookies: This cookie is stored temporary in the client machine. That means, when user closes the browser this session will be deleted from the client machine. This cookie is also called transient cookies.
2)Persistent Cookies: Persistent cookies is stored on the client hard drive in a particular location until it is not expired. That means persistent cookies hold the information until its expiry date and time.
Lets see how to work with cookies.
Like every Server side programming language ASP.NET also have Cookies class to deal with cookies.
To assign the value into cookies
HttpCookie myCookie = new HttpCookie("myCookie"); //Create the instance of HttpCookie class
myCookie.Values.Add("username", "MyUserName"); // Add Key and its value to Cookies
myCookie.Values.Add("password", "MyPassword"); // add Another key and value.
We have stored the value into cookies now we will see how to retirve value from cookies
To retrive the value from Cookies
HttpCookie cookie = Request.Cookies.Get("myCookie");
string UserName = cookie.Values["username"];
string Password = cookie.Values["password"];
NOTE : You should not store any critical information into cookies, as its visible and stored into client machine.
2….
Programming with Cookie
To create a cookie System.Web should be included in web page.
Let's see how to create a cookie with the help of following code

//create a object of cookie
HttpCookie DemoCookie = new HttpCookie("TestCookie")

//Set values in cookies
DemoCookie["Id"] = "123";
DemoCookie["Name"] = "XYZ";

//Add that cookies to response
Response.Cookies.Add(DemoCookie);


That is how we create the cookie. Lets fetch the stored cookie back web page.

HttpCookie DemoCookie = Request.Cookies("TestCookie");
string szName = DemoCookie["Name"] ;


There are two types of cookies exist in ASP.NET.

1. Persistent Cookie
2. Non-Persistent Cookie

Non-Persistent Cookie
Non persistent can also be called as Temporary Cookies. There is no expiry time defined for this type of cookies. They get stored in Browsers Memory. The above example is of Non-Persistent cookie.
When the user closes the browser or if the session times out, the cookie is discarded

Persistent Cookie
Persistent Cookies are also known as permanent cookies. Persistent Cookies are stored in Client hard-drive. It has the expiration dates.
To make the persistent cookie you just have to add expiration date.
When the user visits your site , the browser first examines the collection of cookies for your site. If a cookie has expired, the browser does not send that particular cookie to the server with the page request; instead, the expired cookie is deleted.
following snippet shows you how to add expiration cookie.

DemoCookie.Expires = DateTime.Now.AddDays(4);


Then the issue occurred...How long should the expiration period be for a cookie ?
It depends on what you're using the cookie for , OR it depends on how long your application considers the cookie value to be valid.
You might sometimes write a cookie that expires in seconds or minutes.

Advantages of Cookies

- It's very simple to use and implement.
- Browser's taking care send data.
- For multiple sites cookies, Browser automatically arranges them.


Disadvantages of Cookies
- Size limitation upto (4096 bytes).
- It's store data in simple format so it's not secure
- Number if cookies also limited. Most Browser provides limits of storing cookies is 20.
- It will not work on a high security configuration of browser

Note:
Don't forget that users can clear the cookies on their computer any time. Even if you store cookies with long expiration times, a user might decide to delete all cookies, wiping out any settings you might have stored in cookies.

Summing up
- Cookies are the Clientside State Management technique where we can store data at client side.
- The data will store either on disk or in browser's memory
- Persistent and NonPersistent are the two types of cookies
- we can disabled the cookie.

No comments:

Post a Comment