Solved

C# and cookies

Posted on 2004-08-16
3
216 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

20 Experts available now in Live!

Get 1:1 Help Now