You are on page 1of 3

all thing about cookies in asp.net c# - The Code Project - ASP.NET http://www.codeproject.com/Purgatory/cookies_in_c_.

asp

4,745,922 members and growing! 11,523 Email Password Sign in Join! b Remember me?
c
d
e
f
g
now online. Password problem?
Help!
Home MFC/C++ C# ASP.NET VB.NET Architect SQL All Topics

All Topics, ASP.NET >> ASP.NET >> Purgatory (Beginner) C#


Windows (WinXP), .NET
ASP.NET, Win32, VS
all thing about cookies in asp.net c# Dev
By A.Salem. Posted : 22 Jul 2005
Updated : 9 Sep 2005
this article will help you in knowing what is cookies and how to set,clear and edit them in c# web application,you can use it in Views : 34,835
your web application to facilitate the users login

Note: This is an unedited reader contribution in Purgatory


ANNOUNCEMENTS Advanced Search
Search Articles 6 Go! Sitemap
Visual Studio 2008
competition. Print Broken Article? Bookmark Discuss Send to a friend 21 votes for this
$25,000 in prizes
article.
Office 2007 comp. Win
free travel and passes Popularity: 2.28. Rating: 1.72 out
of 5.
Jobs @ CodeProject
Monthly Competition
Download source files - 23 Kb
Download demo project - 33 Kb

Introduction
a cookies are A very small text file placed on your hard drive by a Web Page
server. It is essentially your identification card, and cannot be executed as code or
deliver viruses. It is uniquely yours and can only be read by the server that gave it
to you

cookies purpose is To tell the server that you returned to that Web page

it may help you by saving your time

If you personalize pages, or register for products or services, a cookie helps


Microsoft remember who you are.

Next time you return, we know to show you the information you requested. Or,
when you register for another product or service, all you need to do is type in your
e-mail address and a password. We then fill in any questions you've already
answered. Of course, if you never register or leave personal information with
Microsoft, then the server only knows that someone with your cookie has returned
to the Web site. You are in charge of deciding whether we know anything about
you. But the more you tell us about yourself, the more we can help you find
information or products you want

how to use it
in order to handle a cookie in your system you should aware of 2 thing:

1-how to create them

2- how to delete them

so,first in order to create a cookie you have 2 variable must be defined as they are
the cookie name and the duration of the cookie before it expired

in order to create a cookie it looks like this one

////////////SET THE COOKIE//////////////


Response.Cookies["test"].Value = UserName.Text ;

Response.Cookies["test"].Expires = DateTime.Now.AddYears(30);

1 de 3 28/11/2007 13:42
all thing about cookies in asp.net c# - The Code Project - ASP.NET http://www.codeproject.com/Purgatory/cookies_in_c_.asp

where test is the cookie name

and the username.text is the textbox that will be used to define the cookies information

in order to delete a cookie use this


Response.Cookies["test"].Expires = DateTime.Now.AddYears(-30);

in order to check whether the cookie is found or not and retrieve the
cookies information use this

/////////check to see if cookie presented//////////


if (Request.Cookies["test"] == null)

TextBox2.Text = "No cookie found";

else

TextBox2 .Text = Request.Cookies["test"].Value;

/////////END check to see if cookie presented//////////

About A.Salem
if you need any details please contact me on protos_time_to_cry@hotmail.com

Click here to view A.Salem's online profile.

Other popular ASP.NET articles:


ASP.NET Popup Control
Highly customizable JavaScript popup control for web
page wrapped in ASP.NET custom control.
NHibernate Best Practices with ASP.NET,
1.2nd Ed.
This article describes best practices for leveraging the
benefits of NHibernate 1.2, ASP.NET, generics and unit
testing together.
Role-based Security with Forms
Authentication
Provides insight and tips on using role-based (groups)
Forms Authentication in ASP.NET, which has only partial
support for roles.
A Session Data Management Tool
Introducing a .NET web service to manage session data
for multiple applications

Add this article to:

[Top] Sign in to vote for this article: Poor n


j
k
l
m j n
k
l
m
n j n
k
l
m j Excellent
j n
k
l
m k
l
m Vote

Note: You must Sign in to post to this message board.


FAQ Message score threshold 3.0 6 Search comments Set Options
View Message View 6 Per page 25 6
Msgs 1 to 1 of 1 (Total: 1) (Refresh) First Prev Next
Subject Author Date
Thank you gagy379 16:01 11 Jul '07

Last Visit: 6:01 Wednesday 28th November, 2007 First Prev Next

General comment News / Info Question Answer Joke / Game Admin

2 de 3 28/11/2007 13:42
all thing about cookies in asp.net c# - The Code Project - ASP.NET http://www.codeproject.com/Purgatory/cookies_in_c_.asp

message

Updated: 9 Sep 2005 Article content copyright A.Salem, 2005


everything else Copyright © CodeProject, 1999-2007.
Web10 | Advertise on The Code Project | Privacy

The Ultimate Toolbox • ASP Alliance • Developer Fusion • Developersdex • DevGuru • Programmers Heaven • Planet Source Code • Tek-Tips Forums •

3 de 3 28/11/2007 13:42

You might also like