Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2005, 01:08 PM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Duplicate cookie names

Hi all,
I'm developing an ASP.NET web application. I'm storing information in cookies like this...

 Dim SSize2Cookie As HttpCookie
 If Request.Cookies("STACKSIZE") Is Nothing Then
      SSize2Cookie = New HttpCookie("STACKSIZE")
      SSize2Cookie.Value = CInt(1) ' integer
      Response.Cookies.Add(SSize2Cookie)
 Else ' cookie already exists
      Response.Cookies("STACKSIZE").Value = CInt(1)
 End If

After this cookie is created, I modify the cookie by overwriting it. When I attempt to modify a cookie, instead of overwriting the previous one, it creates a new one with the exact same name.

Has anyone run into this problem? Any help would be greatly appreciated.

Thanks,
Jill
 
Old May 5th, 2005, 02:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I doubt this will answer your question directly but it might help in solving a cookie problem:

From my experience with web applications, it seems that cookies operate like this:
The browser sends all applicable cookies to the server with the request. These cookies are seen in the Request.Cookies collection. The Response.Cookies collection always starts out empty. If the browser doesn't receive any cookies (i.e. Response.Cookies stays empty) it doesn't do anything. Existing valid cookies in the browser stay put. When you need to send a cookie to the browser, you need to add it too the Response.Cookies collection on every page request.

You can confirm this behavior if you can watch a page in a debugger. When you look at a request that has cookies, the Response.Cookies collection will be empty to start.

Perhaps this is what's causing the conditions you are seeing.

-Peter
 
Old May 5th, 2005, 03:33 PM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. I think my problem is that I was not deleting the cookie before setting a new value. After I deleted it (by setting an expiration date at a date in the past), it is working ok now.
 
Old May 5th, 2005, 03:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should be able to set a new value without deleting it. And actually, if you delete it (by adding it to Response.Cookies with an expiration date) and then adding it, you are negating the effects of the previous "delete" action. If you added then "deleted" you'd delete only because of the way the browser processes cookies.

Not sure why it took what it did to get it to work for you though.

-Peter
 
Old May 6th, 2005, 08:37 AM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello -
I am still stuck. For some reason (I'm not sure why) I have a cookie in my web application that has not been deleted. I have put in this code to list the cookies and then set an expiration date on all those cookies (except for ASP.NET_SessionId). When the page runs, it displays the cookie to be deleted, but after the expiration date is put on the cookie (a date in the past), I expect the cookie to be immediately deleted. Unfortunately, I get a second cookie with the same name, instead of deleting it. Here's my code. Any suggestions would be great. Thanks.
Jill

Dim i
Dim aCookie As HttpCookie
For i = 0 To Request.Cookies.Count - 1
 aCookie = Request.Cookies(i)
 Response.Write("aCookie.Name: '" & aCookie.Name & "'<br>")
 If InStr(aCookie.Name, "SessionId") = 0 Then 'don't delete ASP. NET_SessionId
 ' put an expire date on it for delete
  Response.Write("ready to delete Name: '" & aCookie.Name & "'<br>")
  Response.Cookies(aCookie.Name).Expires = #1/1/2000#
 End If
Next

' now list the current cookies
 For i = 0 To Request.Cookies.Count - 1
  aCookie = Request.Cookies(i)
  Response.Write("after clean-up: aCookie.Name: '" & aCookie.Name & "'<br>")
Next
Response.End()
 
Old May 6th, 2005, 08:58 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Jill,

Remember that the contents of Request.Cookies is what the browser has sent with the request. Any cookies in Response.Cookies are being sent back to the browser. You will not see changes in Request.Cookies until the browser has received the response and then issued another request. You need to finish one round trip and start another for cookie changes to be visible.

-Peter
 
Old May 6th, 2005, 03:33 PM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter -
OK, I did not take this into account. But, I am still having trouble. Here is my code:

If Request.Cookies("STACKSIZE") Is Nothing Then
 ' do nothing here
Else
  aCookie = Request.Cookies("STACKSIZE")
  aCookie.Expires = DateTime.Now.AddDays(-1)' delete previous cookie, if exists
  Response.Cookies.Add(aCookie) ' Response.Cookie makes a round-trip to the server. **This does not delete cookie**
End If

But, ... after this code there are now **2** Stacksize cookies.

Thanks so much for your help so far.
Jill
 
Old May 6th, 2005, 05:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Um, this code will not make a round trip. You need to refresh the page after deleting the cookie.


Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
 
Old May 7th, 2005, 09:25 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by jillsar
 ... after this code there are now **2** Stacksize cookies.

How are you determining this?

When you write a cookie to the response stream, the browser takes it and replaces any existing cookie with it. You can't have two cookies from the browser.

Perhaps an explanation of what you are attempting to accomplish with this cookie would help us understand where you having the problems. Cookies work fine for the right thing. Perhaps what you are trying to use them for could be done a different way that is more suitable.

-Peter
 
Old May 11th, 2005, 09:06 AM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again -
My web application that I am updating had just asp pages. The problem that I have been having is when I started rewriting some of the pages into aspx pages. No problem if a cookie is created by an aspx page and then read by an asp page. The problem occurs if an aspx page creates a cookie, then an asp page updates the cookie, then an aspx page updates the cookie, and finally an asp page tries to read this cookie. The cookie the asp page gets is the cookie value before the aspx page updated the cookie.

I now see that there are **2** cookie files created by IE: one seems to be used by the asp pages and the second by the aspx pages. So, now that I see this, I designed a work-around. The info that needs to be written by both asp and aspx pages I keep in a database. Now it seems to be working fine.

Thanks so much to all of you for your help.
Jill





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie prv299 ASP.NET 2.0 Professional 3 May 29th, 2008 11:26 AM
Cookie problem ps0208 Servlets 1 November 5th, 2007 05:55 AM
Cookie problem Raptor-X ASP.NET 2.0 Basics 0 November 16th, 2006 08:58 AM
Cookie Problem jobiweb ASP.NET 1.0 and 1.1 Basics 0 May 12th, 2005 06:10 AM
Duplicate Element Names carolynk XML 1 September 30th, 2004 09:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.