Solved

C# and cookies

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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