.NET Framework - ASP NET 2.0 Cookie
Asked By icanhelp3
08-May-08 07:08 AM
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..
DateTime.Now.AddDays
(1)
NullReferenceException
(1)
Server.HtmlEncode
(1)
DateTime
(1)
ASP.NET
(1)
ASP NET
(1)
Response.Cookies
(1)
Request.Cookies
(1)
Juan T. Llibre replied...
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/
======================================
icanhelp3 replied...
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 replied...
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/
======================================
Patrice replied...
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
17a3fab7-c885-4fa2-9e93-5fc45bb037d3@v26g2000prm.googlegroups.com...
Patrice replied...
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
discussion : D9051416-6E51-4DA2-9371-B22FD0665AEE@microsoft.com...
Juan T. Llibre replied...
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/
======================================
Juan T. Llibre replied...
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/
======================================
Mark Rae [MVP] replied...
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] replied...
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
Peter Bromberg [C# MVP] replied...
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 --
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
eliza replied to Juan T. Llibre

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.

This is a second post on this as the first never showed up in search. ASP.NET Security Discussions ASPNET security (1) Jerry Weng Microsoft Online Community (1) Silverlight (1) NET Framework (1) ASP.NET (1) RIA (1) WCF (1) Application (1) Cancel this. The original showed up. Was a for posting. From your post, my understanding on this issue is: you want to use ASP.NET security in your Silverlight application. If I am off base, please feel free to let me know. If we want to use ASP.NET Authentication in Silverlight applications, we need to create an ASP.NET Application Service. ASP.NET
de http: / / foto.lupusdw.de) Urlaub macht man in Irland: http: / / www.eaglesnest-bb.com / ASP.NET - German Discussions IsNullOrEmpty (1) HyperLink (1) GridView (1) ASP.NET (1) DataRow (1) Control (1) Visual (1) DataBinding (1) Hallo Lupus, Text = '<%# Container.DataItem( "ReadMore1Text NavigateUrl = '<%# Container.DataItem( "ReadMore1Link" ) %> ' / > HTH - - Tschau, Stefan Microsoft MVP - Visual Developer ASP / ASP.NET http: / / www.asp-solutions.de / - Consulting, Development http: / / www.aspnetzone.de / - ASP.NET Zone, die ASP.NET Community Hallo Stefan, Am 20.05.2010 09:24 schrieb Stefan
publizieren, Falscher Benutzername oder Passwort .NET Framework Sal? alle zusammen In VisualStudio im Men? Projekct-> ASP.NET Configuration habe ich Benutzer und Rollen angelegt. Die dazu n?tige Datenbank wurde im AppData das irgendwie mit der Application ID zusammen? Vielen lieben Dank f?r Hinweise Gruss Tamara ASP.NET - German Discussions SQL Server (1) Visual Studio (1) AccessDataSource (1) SqlDataSource (1) ASP.NET (1) VB.NET (1) IIS (1) Application (1) Hallo Tamara, wie schon ca. 3 Zilliarden Membership Dingern wei? man das nie so genau. - - Tschau, Stefan Microsoft MVP - Visual Developer ASP / ASP.NET http: / / www.asp-solutions.de / - Consulting, Development http: / / www.aspnetzone.de / - ASP.NET Zone, die
framework oder zumindest eine fertige bastelanleitung, wie man das am saubersten und stabilsten implementiert? JMS ASP.NET - German Discussions SQL Server (1) Visual Studio (1) XMLHttpRequest (1) ASP.NET (1) Native (1) Database (1) Load (1) HttpRequest (1) Jedes g?ngige JavaScript-Framework kann das. Bsp:: http: / / api.jquery.com / jQuery.get / - - Gru?, Thomas [MVP ASP / ASP.NET] http: / / www.69grad.de Mit ungef?hr 3 Zeilen javascript Code geht das auch: / / <![CDATA dann die eierlegende Wollmilchsau unter den JavaScript Frameworks. - - Tschau, Stefan Microsoft MVP - Visual Developer ASP / ASP.NET http: / / www.asp-solutions.de / - Consulting, Development http: / / www.aspnetzone.de / - ASP.NET Zone, die
das nun von vb.net aus? Danke schon mal, Gru? Hans VB.NET - German Discussions DateTime.Today.AddDays (1) OleDb.OleDbDataReader (1) System.Text.StringBuilder (1) OleDb.OleDbCommand (1) Visual Studio 2008 (1 was bringen: http: / / support.microsoft.com / kb / 306682 - - Tschau, Stefan Microsoft MVP - Visual Developer ASP / ASP.NET http: / / www.asp-solutions.de / - Consulting, Development http: / / www.aspnetzone.de / - ASP.NET Zone, die ASP.NET Community Hallo Hans, Eine Datenbank aufrufen? Ich denke eher eine Verbindung (Connection) zu einer