Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > ASP / ASP.NET

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

Setting A Cookie in ASP.NET


On 18th October, 2008
Thumbs up Setting A Cookie in ASP.NET

Show Printable Version Email this Page Subscription Add to Favorites Copy Setting A Cookie in ASP.NET link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

What Is A Cookie



A cookie is a small text file sent to a web user's computer by a website. A cookie can be used to identify that user to the website on their next visit. Common uses include remembering login data, user preferences, and providing favorites lists.

How Does A Cookie Work



HTTP cookies are used by Web servers to differentiate users and to maintain data related to the user during navigation, possibly across multiple visits. When an user visits/logs in to go4expert.com the web server sets a cookie to track the user's last visted time, and some ID to track the specific visitor. Many websites also use cookies for personalization based on users' preferences.

How Much Can We Store In A Cookie



Cookie specifications suggest that browsers should support a minimal number of cookies or amount of memory for storing them. In particular, an internet browser is expected to be able to store at least 300 cookies of four kilobytes each (both name and value count towards this 4 kilobyte limit), and at least 20 cookies per server or domain, so it is not a good idea to use a different cookie for each variable that has to be saved. It's better to save all needed data into one single cookie.

Setting A Cookie in ASP.Net



Setting cookies with a ASP.Net script is easy, we just need to use the HttpCookie class in the System.Web namespace. Let's look at an example on creating a cookie with ASP.Net

Code: C#
//Creating the cookie
 HttpCookie sampleCookie = new HttpCookie("User");
 sampleCookie.Values.Add("Name", strUserName);
 sampleCookie.Values.Add("CountryCode", strCountry);
 sampleCookie.Expires = #01/01/2009#; // set the expiry
 Response.Cookies.Add(sampleCookie); // Add it to the header
 
 //Getting a cookie value
 String sGetCookie;
 strUserName = Request.Cookies("User")("Name").ToString();
 strUserCountry = Request.Cookies("User")("CountryCode").ToString();


References



http://www.joedolson.com/glossary.php
http://en.wikipedia.org/wiki/HTTP_cookie
Old 11-15-2008, 11:06 PM   #2
Newbie Member
 
JGRobinson's Avatar
 
Join Date: Nov 2008
Location: UK
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
JGRobinson is on a distinguished road

Re: Setting A Cookie in ASP.NET


Excellent and very clear article - The only thing which I would add is that by default cookies are only sent for the domain which created them

Therefore if the cookie was written by test.go4expert.com it could not be read by forums.go4expert.comThe way of fixing this is to set the domain of the cookie to the base domain, then both forums.go4expert.com and test.go4expert.com could access the information

The code to add would be :-

Code:
Response.Cookies("domain").Value = DateTime.Now.ToString()
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "go4expert.com"
I've tripped over this before and thought it was worth clarifyng.
JGRobinson is offline   Reply With Quote
Old 11-18-2008, 10:35 AM   #3
Newbie Member
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
zaanrider is on a distinguished road

Re: Setting A Cookie in ASP.NET


Very nice artical..thanks
zaanrider is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Caching Pages in ASP.NET coderzone ASP / ASP.NET 0 04-18-2007 09:59 PM
A fantastic IDE (editor) for working with ASP.NET pradeep Windows 0 03-15-2007 12:51 PM
Exception Handling Techniques in ASP.NET Sanskruti ASP / ASP.NET 0 01-09-2007 04:50 PM

 

All times are GMT +5.5. The time now is 05:35 AM.