Solved

C# and cookies

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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