Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ASP NET 2.0 Cookie

3 views
Skip to first unread message

icanh...@gmail.com

unread,
May 6, 2008, 12:11:53 PM5/6/08
to
I have a C# utility that runs in ASP website. When user logs in the
ASP code writes the cookie using Response.Cookie. I try to read the
same cookie in C#. I am unable to read it in C# code.

I am using Request.Cookies["cookiename"].Value.ToString(). I can read
the same cookie in ASP code.
If I say Request.Cookies("CookieName") in ASP that works fine..


Juan T. Llibre

unread,
May 6, 2008, 1:06:45 PM5/6/08
to
re:
!> I am using Request.Cookies["cookiename"].Value.ToString()

Try either of these approaches :

1.

if(Request.Cookies["cookieName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value);

2.
if(Request.Cookies["cookieName"] != null)
{
HttpCookie aCookie = Request.Cookies["cookieName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

Before trying to get the value of a cookie, you should make sure that the cookie exists;
if the cookie does not exist, you will get a NullReferenceException exception.

Notice also that the HtmlEncode method was called to encode the contents of a cookie
before displaying it in the page. This makes certain that a malicious user has not added
executable script into the cookie.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
<icanh...@gmail.com> wrote in message news:17a3fab7-c885-4fa2...@v26g2000prm.googlegroups.com...

icanh...@gmail.com

unread,
May 6, 2008, 1:29:30 PM5/6/08
to
I know how to read the cookie. The problem is when I try to read it in
C# it always returns null. In ASP it returns the value of the cookie.

Juan T. Llibre

unread,
May 6, 2008, 1:37:45 PM5/6/08
to
re:
!> I know how to read the cookie.
!> The problem is when I try to read it in C# it always returns null.

Then, either the code you're using doesn't read the cookie properly,
or the cookie isn't being set correctly.

Have you tried the code I posted to read cookies ?

As for setting cookies, here's 2 sample C# code fragments :

1.
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

2.
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

How are you setting the cookie ?
Are you expiring the cookie ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================

<icanh...@gmail.com> wrote in message news:111448ed-1b1c-4f14...@z16g2000prn.googlegroups.com...

Patrice

unread,
May 6, 2008, 1:42:01 PM5/6/08
to
I'm really not sure you can do that... ASP And ASP.NET are separate
engines... I'm even surprised you don't get an exception as from the point
of view of the C# utility (how do you run this one ? This is exposed as a
com object ?) there is IMO no current (ASP.NET) request...

You may want to elaborate a bit on your architecture and/or what you are
trying to do...

--
Patrice

<icanh...@gmail.com> a écrit dans le message de groupe de discussion :
17a3fab7-c885-4fa2...@v26g2000prm.googlegroups.com...

Patrice

unread,
May 6, 2008, 1:50:41 PM5/6/08
to
Or do you mean you just transfer control from an ASP page to an ASPX page as
Juan seems to have understood from your description ?

As you talked about a "C# utility" rather than about an ASP.NET page, I was
thinking that this "C# utility" was directly called from the ASP page during
the same request. As i said earlier you may want to elaborate a bit on your
architecture so that we are 100 % sure about how ASP and ASP.NET interacts
in your design...

--
Patrice

"Patrice" <http://www.chez.com/scribe/> a écrit dans le message de groupe de
discussion : D9051416-6E51-4DA2...@microsoft.com...

Juan T. Llibre

unread,
May 6, 2008, 2:48:02 PM5/6/08
to
Cookies should be able to read regardless
of the language/platform used to write them.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================

"Patrice" <http://www.chez.com/scribe/> wrote in message news:D9051416-6E51-4DA2...@microsoft.com...

Juan T. Llibre

unread,
May 6, 2008, 6:25:59 PM5/6/08
to
Typo alert...

That should have been :

Cookies should be able to be read regardless


of the language/platform used to write them.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================

"Juan T. Llibre" <nomail...@nowhere.com> wrote in message news:%23VT45m6...@TK2MSFTNGP02.phx.gbl...

Mark Rae [MVP]

unread,
May 6, 2008, 7:15:30 PM5/6/08
to
"Juan T. Llibre" <nomail...@nowhere.com> wrote in message
news:eYcesg8r...@TK2MSFTNGP06.phx.gbl...

> Cookies should be able to be read regardless
> of the language/platform used to write them.

Correct, though some languages like C# are case-sensitive, which may be the
issue here...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Peter Bromberg [C# MVP]

unread,
May 7, 2008, 8:08:43 PM5/7/08
to
Make sure the Path property of your cookie is set. , e.g. "/". There are
minor inconsistencies in classic ASP cookies and ASP.NET cookies.
Peter
<icanh...@gmail.com> wrote in message
news:111448ed-1b1c-4f14...@z16g2000prn.googlegroups.com...

Peter Bromberg [C# MVP]

unread,
May 7, 2008, 8:12:25 PM5/7/08
to
To be more specific:

Cookie Interop - ASP -->ASPX:

There is a change in cookie behavior between ASP and ASPX. In brief: If you
do NOT
specifically set a cookie path, ASP will automatically set the path to:
your web application's name --
"/AppName"
If you do NOT specifically set a path, ASPX will automatically set the path
to:
your server name --
"/"

To make ASP behave like ASPX, then add --
Response.Cookies("...").Path = "/" -- to your ASP code

To make ASPX behave like ASP, then add --
MyCookieObject.Path = "/AppName" -- to your ASPX code.

--Peter
<icanh...@gmail.com> wrote in message
news:111448ed-1b1c-4f14...@z16g2000prn.googlegroups.com...

eliza

unread,
Apr 28, 2010, 7:55:51 AM4/28/10
to
What is a Cookie ?

A Cookie is an object is used to store the sort amount of data onto client end. It executes on server and resides on client browser.

How does Cookie work ?

when users request a page from your site, your application sends not just a page, but a cookie containing some data. When the user's browser gets the page, the browser also gets the cookie.Later, the user requests a page from your site again.If the cookie exists, the browser sends the cookie to your site along with the page
request.

What are different type of Cookie ?

Session Cookie : This Cookie will expire once the browser is closed.
Persistent Cookie : This Cookie will exists in browser till the time of expire.
How much can we store in a Cookie ?

Cookie specifications suggest that browsers should support a minimal number of cookies . 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.

Juan T. Llibre wrote:

Re: ASP NET 2.0 Cookie
06-May-08

re:
!> I am using Request.Cookies["cookiename"].Value.ToString()

Try either of these approaches :

1.

if(Request.Cookies["cookieName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value);

2.
if(Request.Cookies["cookieName"] != null)
{
HttpCookie aCookie = Request.Cookies["cookieName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

Before trying to get the value of a cookie, you should make sure that the cookie exists;
if the cookie does not exist, you will get a NullReferenceException exception.

Notice also that the HtmlEncode method was called to encode the contents of a cookie
before displaying it in the page. This makes certain that a malicious user has not added
executable script into the cookie.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/

foros de asp.net, en espa?ol : http://asp.net.do/foros/


======================================
<icanh...@gmail.com> wrote in message news:17a3fab7-c885-4fa2...@v26g2000prm.googlegroups.com...

Previous Posts In This Thread:

On Tuesday, May 06, 2008 1:06 PM
Juan T. Llibre wrote:

Re: ASP NET 2.0 Cookie


re:
!> I am using Request.Cookies["cookiename"].Value.ToString()

Try either of these approaches :

1.

if(Request.Cookies["cookieName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value);

2.
if(Request.Cookies["cookieName"] != null)
{
HttpCookie aCookie = Request.Cookies["cookieName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

Before trying to get the value of a cookie, you should make sure that the cookie exists;
if the cookie does not exist, you will get a NullReferenceException exception.

Notice also that the HtmlEncode method was called to encode the contents of a cookie
before displaying it in the page. This makes certain that a malicious user has not added
executable script into the cookie.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/

foros de asp.net, en espa?ol : http://asp.net.do/foros/


======================================
<icanh...@gmail.com> wrote in message news:17a3fab7-c885-4fa2...@v26g2000prm.googlegroups.com...

On Tuesday, May 06, 2008 1:37 PM
Juan T. Llibre wrote:

Re: ASP NET 2.0 Cookie


re:
!> I know how to read the cookie.
!> The problem is when I try to read it in C# it always returns null.

Then, either the code you're using doesn't read the cookie properly,
or the cookie isn't being set correctly.

Have you tried the code I posted to read cookies ?

As for setting cookies, here's 2 sample C# code fragments :

1.
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

2.
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

How are you setting the cookie ?
Are you expiring the cookie ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espa?ol : http://asp.net.do/foros/
======================================
<icanh...@gmail.com> wrote in message news:111448ed-1b1c-4f14...@z16g2000prn.googlegroups.com...

On Tuesday, May 06, 2008 1:42 PM
Patrice wrote:

I'm really not sure you can do that... ASP And ASP.NET are separate engines...
I'm really not sure you can do that... ASP And ASP.NET are separate
engines... I'm even surprised you don't get an exception as from the point
of view of the C# utility (how do you run this one ? This is exposed as a
com object ?) there is IMO no current (ASP.NET) request...

You may want to elaborate a bit on your architecture and/or what you are
trying to do...

--
Patrice

<icanh...@gmail.com> a ?crit dans le message de groupe de discussion :
17a3fab7-c885-4fa2...@v26g2000prm.googlegroups.com...

On Tuesday, May 06, 2008 1:50 PM
Patrice wrote:

Or do you mean you just transfer control from an ASP page to an ASPX page as
Or do you mean you just transfer control from an ASP page to an ASPX page as
Juan seems to have understood from your description ?

As you talked about a "C# utility" rather than about an ASP.NET page, I was
thinking that this "C# utility" was directly called from the ASP page during
the same request. As i said earlier you may want to elaborate a bit on your
architecture so that we are 100 % sure about how ASP and ASP.NET interacts
in your design...

--
Patrice

"Patrice" <http://www.chez.com/scribe/> a ?crit dans le message de groupe de
discussion : D9051416-6E51-4DA2...@microsoft.com...

On Tuesday, May 06, 2008 2:48 PM
Juan T. Llibre wrote:

Cookies should be able to read regardlessof the language/platform used to
Cookies should be able to read regardless


of the language/platform used to write them.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/

foros de asp.net, en espa?ol : http://asp.net.do/foros/


======================================
"Patrice" <http://www.chez.com/scribe/> wrote in message news:D9051416-6E51-4DA2...@microsoft.com...

On Tuesday, May 06, 2008 6:25 PM
Juan T. Llibre wrote:

Typo alert...
Typo alert...

That should have been :

Cookies should be able to be read regardless


of the language/platform used to write them.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/

foros de asp.net, en espa?ol : http://asp.net.do/foros/
======================================
"Juan T. Llibre" <nomail...@nowhere.com> wrote in message news:%23VT45m6...@TK2MSFTNGP02.phx.gbl...

On Tuesday, May 06, 2008 7:15 PM
Mark Rae [MVP] wrote:

Re: ASP NET 2.0 Cookie


Correct, though some languages like C# are case-sensitive, which may be the
issue here...

On Wednesday, May 07, 2008 8:08 PM


Peter Bromberg [C# MVP] wrote:

Make sure the Path property of your cookie is set. , e.g. "/".
Make sure the Path property of your cookie is set. , e.g. "/". There are
minor inconsistencies in classic ASP cookies and ASP.NET cookies.
Peter

On Wednesday, May 07, 2008 8:12 PM
Peter Bromberg [C# MVP] wrote:

Re: ASP NET 2.0 Cookie
To be more specific:

Cookie Interop - ASP -->ASPX:

There is a change in cookie behavior between ASP and ASPX. In brief: If you
do NOT
specifically set a cookie path, ASP will automatically set the path to:
your web application's name --
"/AppName"
If you do NOT specifically set a path, ASPX will automatically set the path
to:
your server name --
"/"

To make ASP behave like ASPX, then add --
Response.Cookies("...").Path = "/" -- to your ASP code

To make ASPX behave like ASP, then add --
MyCookieObject.Path = "/AppName" -- to your ASPX code.

--Peter
<icanh...@gmail.com> wrote in message
news:111448ed-1b1c-4f14...@z16g2000prn.googlegroups.com...

On Thursday, May 08, 2008 7:08 AM
icanhelp3 wrote:

ASP NET 2.0 Cookie


I have a C# utility that runs in ASP website. When user logs in the
ASP code writes the cookie using Response.Cookie. I try to read the
same cookie in C#. I am unable to read it in C# code.

I am using Request.Cookies["cookiename"].Value.ToString(). I can read
the same cookie in ASP code.
If I say Request.Cookies("CookieName") in ASP that works fine..

On Thursday, May 08, 2008 7:08 AM
icanhelp3 wrote:

I know how to read the cookie.
I know how to read the cookie. The problem is when I try to read it in
C# it always returns null. In ASP it returns the value of the cookie.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 4.0 In a Nutshell [O'Reilly]
http://www.eggheadcafe.com/tutorials/aspnet/6dc05c04-c7f9-40cc-a2da-88dde2e6d891/book-review-c-40-in-a.aspx

0 new messages