ASP NET 2.0 Cookie

I

icanhelp33

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..
 
J

Juan T. Llibre

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.
 
I

icanhelp33

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.
 
J

Juan T. Llibre

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 ?
 
J

Juan T. Llibre

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





Patrice said:
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

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..
 
J

Juan T. Llibre

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 said:
Cookies should be able to read regardless
of the language/platform used to write them.





Patrice said:
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

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..
 
P

Peter Bromberg [C# MVP]

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
 
P

Peter Bromberg [C# MVP]

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
 
E

eliza

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 cooki
before displaying it in the page. This makes certain that a malicious user has not adde
executable script into the cookie




Previous Posts In This Thread:

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 cooki
before displaying it in the page. This makes certain that a malicious user has not adde
executable script into the cookie




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 ?





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

<[email protected]> a ?crit dans le message de groupe de discussion :
(e-mail address removed)...

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 : (e-mail address removed)...

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.





"Patrice" <http://www.chez.com/scribe/> wrote in message
Typo alert...
Typo alert...

That should have been :

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






Re: ASP NET 2.0 Cookie
Correct, though some languages like C# are case-sensitive, which may be the
issue here...


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

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

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

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..

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/tutorial...-a2da-88dde2e6d891/book-review-c-40-in-a.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top