By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,503 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,503 IT Pros & Developers. It's quick & easy.

Help needed !! Permanent cookies not working

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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);
--

Thanks,
Yunus Emre ALP÷ZEN
BSc, MCAD.NET

"Raghu Raman" <ra************@rediffmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #2

P: n/a

"Raghu Raman" <ra************@rediffmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***


Try changing ht.Expires.AddDays(2); to ht.expires =
datetime.now.adddays(2)
Nov 19 '05 #3

P: n/a
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.

"Raghu Raman" <ra************@rediffmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.