C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Guide To ASP.NET Cookies


Posted Date: 30 Jun 2009    Resource Type: Articles    Category: .NET Framework
Author: pradeepMember Level: Silver    
Rating: 1 out of 5Points: 3



What are Cookies ?

Cookies are the small files that are created on the client's system or client browser memory (if temporary). Its use for State management that I have already discuss on my view state article. So we can store small piece of information in a client system and we can use it when we needed. Most interesting thing is that Its works transparently with the user. It can be easily used any where of you web application. Cookies store information in a plain text format. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences , Password Remember Option etc. Cookies has many advantages and disadvantages. I will comes to this points , but first have a look how cookies are started.

How Cookies are started ?
When client request to the server, server send the cookies into client . The same cookies can be referred for subsequent request. As for example, if codeproject.com stores session id as a cookies, when any client hits first times on the server, server generates the session id and send it as a cookies to client.


Advantages of Cookies

Following are main advantages of using cookies in web application:

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

Main disadvantages of cookies are:

Its store data in a simple text format. so it's not secure at all.

There is a size limit of cookies data ( 4096 bytes / 4KB).

Number if cookies also limited. Most Browser provides limits of storing cookies is 20. If new cookies came, it will discard the old one. Some of browser support up to 300.

We need to configure browser. It will not work on a high security configuration of browser.
How to create cookies ?


For working with cookies we need to use namespace System.web



Now , have a look, on the code , that how can we create a cookies and add it with web response .3



The cookies which has been created will persist , until browser has been closed. we can persist the cookies. But how? Just after few point I have discussed it.

How to Read data from cookies ?

Now , its times to retrieve data from cookies. Ok, before reading cookies, first of all we need to check whether a cookies was found or not. "Its always good practice to check cookie before read it, because is browser is disable cookies.

What is Persistent and Non Persistent Cookies ?

We can classified cookies in two way,

Persistent Cookies

Non Persistent Cookies

Persistent Cookies : This can be called as permanent cookies, which is stored in client hard-drive until it expires . persistent cookies should have set with expiration dates. Sometimes its stays until the user deletes the cookie. Persistent cookies are used to collect identifying information about the user from that system. I have discuss about the creation of persistent cookies on "How to make Persist Cookies ?" section.

Non Persistent Cookies : This can be called as Temporary Cookies. If there is no expires time defined then the cookie is stored in browser memory . The Example which I have given already its a Non-Persistent Cookies.

Therefore there is no difference between modifying persistent or non-persistent cookies. Only difference between them are Persistent cookies should have an Expatriation time defined within it.

How to make Persistent Cookies ?

I have already given an example of non-persistent cookies, For Persistent cookies we need only add to expiry times of cookies. In that given code I have added Expire time to 5 days. Just check the example.

Collapse
//Creting a Cookie Object
HttpCookie _userInfoCookies = new HttpCookie("UserInfo");

//Setting values inside it
_userInfoCookies["UserName"] = "Abhijit";
_userInfoCookies["UserColor"] = "Red";
_userInfoCookies["Expire"] = "5 Days";

//Adding Expire Time of cookies
_userInfoCookies.Expires = DateTime.Now.AddDays(5);

//Adding cookies to current web response
Response.Cookies.Add(_userInfoCookies);
Now , Looks the most interesting things that where they are store in hard drive.






Responses

Author: Prasoon Kumar    01 Jul 2009Member Level: Silver   Points : 2
Hi

Pradeep ...

nice article but can you elaborate more on "Most Browser provides limits of storing cookies is 20. If new cookies came, it will discard the old one. Some of browser support up to 300."


We will like to know more about which browser/version

specifically supports what ?

Also please analyze how it differs on diffrent OS/mobile

platforms.

Regards

Prasoon.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Cookies  .  Basics of cookies  .  Asp.net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: interview questions
Previous Resource: Best practices to Improve your .net codes performance
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use