Solved

How to set Cookie.Expire time?

Posted on 2004-08-05
6
715 Views
Last Modified: 2012-08-13
Hoi all...
I have trouble with the following code:
--------
                  DateTime dtNow = DateTime.Now;
                  TimeSpan tsMinute = new TimeSpan(365, 0, 0, 0);
                  HttpCookie myCookie = new HttpCookie(cookieName);
                  myCookie.Expires = dtNow + tsMinute;
                  myCookie["memberID"] = iMemberID.ToString();
                  myCookie["memberNick"] = sMemberNick;
                  myCookie["memberPassword"] = sMemberPassword;
                  this.Response.Cookies.Add(myCookie);
----
Now I check, what the expire time for the cookie is:
----
                  myCookie = inPage.Request.Cookies[cookieName];
                  Response.Write("this cookie expires: "+myCookie.Expires);
----
I always get:
01.01.0001 00:00:00

The idea how to set the cookie is taken from here:
http://www.csharphelp.com/archives/archive179.html

Can someone tell me how I tell my cookie to life for 365 days from now?

Thank you.
0
Comment
Question by:Smoerble
6 Comments
 
LVL 2

Expert Comment

by:mahanatti
Comment Utility
0
 

Author Comment

by:Smoerble
Comment Utility
This contains
---
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires =dtExpiry;
---
I did that too, same result as above: expire time seems not to be correct.
Any other idea?
0
 

Author Comment

by:Smoerble
Comment Utility
Update:
I checked what the browser says about the cookie:
the date is set correctly... so the question is, why doesn't it display the correct date in my code?
0
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.

 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 150 total points
Comment Utility
Hmmm... I am wondering if you are cheking your cookie after another page hit?  If you are checking your cookie after setting it you are looking at the wrong object.  You set your cookie on Response and check on Request.  Also, if cookies are turned off on the browser then this may happen.
0
 
LVL 9

Accepted Solution

by:
s_sansanwal earned 350 total points
Comment Utility
0
 

Author Comment

by:Smoerble
Comment Utility
Ok, as stated in
http://www.dotnet247.com/247reference/msgs/48/240266.aspx
the cookie does not send back the correct dateTime when using ....Expires.
Thank you.

If anybody has any other idea, please feel free to share ;)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

11 Experts available now in Live!

Get 1:1 Help Now