473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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..
Jun 27 '08 #1
10 1673
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/
======================================
<ic********@gmail.comwrote in message news:17**********************************@v26g2000 prm.googlegroups.com...
>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..


Jun 27 '08 #2
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.

Jun 27 '08 #3
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/
======================================
<ic********@gmail.comwrote in message news:11**********************************@z16g2000 prn.googlegroups.com...
>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.


Jun 27 '08 #4
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

<ic********@gmail.coma écrit dans le message de groupe de discussion :
17**********************************...oglegroups.com...
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..

Jun 27 '08 #5
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 : D9**********************************@microsoft.com...
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

<ic********@gmail.coma écrit dans le message de groupe de discussion :
17**********************************...oglegroups.com...
>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..

Jun 27 '08 #6
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:D9**********************************@microsof t.com...
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

<ic********@gmail.coma écrit dans le message de groupe de discussion :
17**********************************...oglegroups.com...
>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..


Jun 27 '08 #7
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" <no***********@nowhere.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
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:D9**********************************@microsof t.com...
>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

<ic********@gmail.coma écrit dans le message de groupe de discussion :
17**********************************...oglegroups.com...
>>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..



Jun 27 '08 #8
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:eY**************@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

Jun 27 '08 #9
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
<ic********@gmail.comwrote in message
news:11**********************************@z16g2000 prn.googlegroups.com...
>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.
Jun 27 '08 #10
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
<ic********@gmail.comwrote in message
news:11**********************************@z16g2000 prn.googlegroups.com...
>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.
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
12
by: chrism | last post by:
Hello, I have a pop-up window that I would like to appear in front of the browser home page when a user opens IE. Problem is, I'd like it to never appear again if the user navigates back to the...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
2
by: kelly.pearson | last post by:
Is this a bug? I am trying to write a cookie that can be accessed by various .Net applications on our domain. However, whenever I add the domain property to the cookie, no errors get thrown but...
5
by: cbhoem | last post by:
Hi - I am trying my hand at python cookies. I'm confused about a few things though. Do the python cookies get written to a cookies text file? I have simple code below -- I see the cookie in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.