Solved

C# and cookies

Posted on 2004-08-16
3
217 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
  • 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now