Help needed !! Permanent cookies not working

R

Raghu Raman

Hi,

I want to store a cookie peemanently for 2 days.From msdn article, i
have coded like this.It is working when i run my application .But if i
close my browser and restart my application , i could not find the
stored cookie,it was gone .

my code
-----------------

dt=DateTime.Now;
ckdate=dt.ToString("dd-MMM-yyyy",DateTimeFormatInfo.InvariantInfo);
sid=Session.SessionID.ToString();
HttpCookie ht=new HttpCookie("usercook",ckdate + "^" + sid);
ht.Expires.AddDays(2);
Response.Cookies.Add(ht);
--------------------------
if(Request.Cookies["usercook"]!=null) then it shows null.

Even i close my app and restart again ,i need the value that i stored
last time in the cookie.Pls tell my ,how to store a permanent cookie.

Regards
Raghu
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

You always store your cookie as a new one. You should name it as a constant.
Your cookie name is changing continously. A Sample implementation copied
from Quickstart is given below;

HttpCookie cookie = new HttpCookie("preferences");
cookie.Values.Add("ForeColor",ForeColor.Value);
...
cookie.Expires = DateTime.MaxValue; // Never Expires
Response.AppendCookie(cookie);
 
V

vMike

Raghu Raman said:
Hi,

I want to store a cookie peemanently for 2 days.From msdn article, i
have coded like this.It is working when i run my application .But if i
close my browser and restart my application , i could not find the
stored cookie,it was gone .

my code
-----------------

dt=DateTime.Now;
ckdate=dt.ToString("dd-MMM-yyyy",DateTimeFormatInfo.InvariantInfo);
sid=Session.SessionID.ToString();
HttpCookie ht=new HttpCookie("usercook",ckdate + "^" + sid);
ht.Expires.AddDays(2);
Response.Cookies.Add(ht);
--------------------------
if(Request.Cookies["usercook"]!=null) then it shows null.

Even i close my app and restart again ,i need the value that i stored
last time in the cookie.Pls tell my ,how to store a permanent cookie.

Regards
Raghu

Try changing ht.Expires.AddDays(2); to ht.expires =
datetime.now.adddays(2)
 
G

gabe garza

Don't rely on cookies being stored permanently. They can be cleared at the
browser at any time.
In FireFox there's an option to clear cookies when exiting the browser. In
IE I can clear out the cookies if I choose.

Another thing to keep in mind, cookies are not computer specific. It's a
combination of computer/browser
mycomputer/IE - own set of cookies
mycomputer/FireFox - own set of cookies
mycomputer/Netscape - own set of cookies
mycomputer/Safari - own set of cookies

If your relying on a cookie for a user, that's not going to work if the user
decides to go to another computer or even another browser on the same
computer.
What you should do is save the information on the host computer, then when
the user logs into your system, read from a database or ??? to retrieve user
specific information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top