Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# and cookies

Posted on 2004-08-16
3
Medium Priority
?
227 Views
Last Modified: 2008-03-06

Hello, new to .Net. I'm trying to just check for a cookie and use or store information based off whether or not an existing cookie already exists. The problem I'm having is that my application will not recognize an existing cookie, and each time I build to test my app it just creates a new one. Also, I can not find this new cookie anywhere on my machine, especially where I would expect to find cookies. I've checked everywhere.

I've restarted IIS on my machine as well as rebooted just in case something was up there. Can't put my finger on exactly what's happening, although somehow I suspect that the cookie information is only getting set within the application, and not on my local box to see the next time around??

Here is the simple page in question. What I'm doing here is this class represents a component that will be inherited on subsequent pages for easy code reuse defining global variables and session information. I would expect that after I've run and tested this the first time, on the second time around it would tell me that the cookie is not null, and it does not and continues as if it were not there.

using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Web;

namespace Com.Components
{
      public class BaseCode : System.Web.UI.Page
      {
            override protected void OnInit(EventArgs e)
            {
                  if (Request.Cookies["name"] != null)
                  {
                        Response.Write("cookie is not null");
                  }
                  else
                  {
                        Response.Write("cookie is null");
                        Response.Cookies["name"].Value = "this is my name";
                  }
                  Response.Write("<br>cookie value: " + Request.Cookies["name"].Value);
                  
                  base.OnInit(e);
            }
      }
}


thanks in advance!!



0
Comment
Question by:animated405
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11813315
If you use the same Explorer window and hit "Refresh" it should behave as you expect.  You are creating a session cookie that only lives in the IE session you created it in.  If you do "File->New->Window" the new window will work too.  Once you close those windows, however, the cookie is gone.  If you set the expiration date (Request.Cookies["name"].Expires) to a time in the future, the cookie will show up as a file in the directory where your cookies go (usually Temporary Internet Files).
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11813358
There's some sample code at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpcookieclassexpirestopic.asp

that shows how to set the expiration.  It's pretty simple - set expiration for 10 minutes from now:

   DateTime dt = DateTime.Now;
   TimeSpan ts = new TimeSpan(0,0,10,0);
 
   MyCookie.Expires = dt.Add(ts);  // Could replace MyCookie with Response.Cookies["name"]
0
 

Author Comment

by:animated405
ID: 11813466

Thanks for the quick reply. I was thinking that if there was no expiration set then the cookie was persistant until it was deleted.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question