473,799 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1699
re:
!I am using Request.Cookies["cookiename "].Value.ToString ()

Try either of these approaches :

1.

if(Request.Cook ies["cookieName "] != null)
Label1.Text = Server.HtmlEnco de(Request.Cook ies["cookieName "].Value);

2.
if(Request.Cook ies["cookieName "] != null)
{
HttpCookie aCookie = Request.Cookies["cookieName "];
Label1.Text = Server.HtmlEnco de(aCookie.Valu e);
}

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 NullReferenceEx ception 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********@gma il.comwrote in message news:17******** *************** ***********@v26 g2000prm.google groups.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.Cookie s["userName"].Value = "patrick";
Response.Cookie s["userName"].Expires = DateTime.Now.Ad dDays(1);

2.
HttpCookie aCookie = new HttpCookie("las tVisit");
aCookie.Value = DateTime.Now.To String();
aCookie.Expires = DateTime.Now.Ad dDays(1);
Response.Cookie s.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********@gma il.comwrote in message news:11******** *************** ***********@z16 g2000prn.google groups.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********@gma il.coma écrit dans le message de groupe de discussion :
17************* *************** **...legroup s.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************* *************** **...icrosof 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********@gma il.coma écrit dans le message de groupe de discussion :
17************* *************** **...legroup s.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******** *************** ***********@mic rosoft.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********@gma il.coma écrit dans le message de groupe de discussion :
17************* *************** **...legroup s.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.comwrot e in message news:%2******** ********@TK2MSF TNGP02.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******** *************** ***********@mic rosoft.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********@gm ail.coma écrit dans le message de groupe de discussion :
17************* *************** **...legroup s.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.comwrot e in message
news:eY******** ******@TK2MSFTN GP06.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********@gma il.comwrote in message
news:11******** *************** ***********@z16 g2000prn.google groups.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

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

Similar topics

4
5297
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 cookie. Here is the code I have now. In the header, I have the following: <SCRIPT language="JavaScript"> var cookieStatus; if (document.cookie.length > 0) { cookieStatus = 'Cookie exists with value ' + document.cookie; } else {
12
18018
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 home page during their time using the browser. However, if the user closes the browser, then reopens, the pop-up should appear again. (you may have guessed that this will be used for public access pc's.) I want to try as best I can to catch...
5
3316
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 document.cookie variable combine all cookie key=value pairs? All of the examples I've seen discuss referencing a specific cookie. I don't see how this is done. Cookies are usually named by the domain. If I want to reference a specific cookie, do I...
4
3824
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"> if(document.cookie.indexOf("beenHere1=true")!=-1) else
9
4505
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 in a cookie and a small bullit shown in front of the hyperlink, so when I reload the page I can immediately see which hyperlinks I already visited that day.
3
11128
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 behind and don't use a domain then I can not change or remove that cookie's value on the client. If I subsequently create the cookie again in the codebehind then I actually end up with TWO cookies with the same name in the response. The cookie...
1
6719
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 simply pass the values in a cookie to the querystring that would be even easier, but from what I've been able to tell, cookie values can't be passed to a querystring. I'm sure the answer will help alot of others who are using this script, and would...
6
2703
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 problem is how do I handle the "path" parameter in cookie. First, the sequence start at http://www.testServer1.com/TestApp/page1.htm, and a cookie is set at
2
2855
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 the cookie doesn't get written. I am trying this from my localhost using vs2003, .net framework 1.1, windows xp prof. Sample code I am testing with is below. Public Class WebForm1 Inherits System.Web.UI.Page
5
3165
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 my HTTP header but do not get anything in the cookie text file. I'm working on linux. print "Content-type: text/html" cookie = Cookie.SimpleCookie()
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.