Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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
  #1 (permalink)  
Old November 7th, 2007, 05:21 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Cookie Questions

Page 1 sets a cookie. The expiration date for the cookie is an hour in the future. I have verified the cookie exists and the expiration date is indeed an hour in the future right before the redirect to page 2. Page 2 then tries to read the same cookie as the very first line in page load, but can't find it! Both pages are in the same project. I am running in VS 2005.

Can anybody explain why this isn't working or what else I need to do to make it work as expected?
  #2 (permalink)  
Old November 8th, 2007, 04:49 AM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Browser settings? See if there's something fishy or try with other browser. If still doesn't work, could you provide the code where you set the cookie and where you try to read it.

  #3 (permalink)  
Old November 8th, 2007, 09:15 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Sorry, I should've mentioned there is only one browser of concern, IE 6.0. Cookies are enabled.

The code couldn't be simpler:

Logic behind button on page 1:


...

     SavePINCookie()

     ...

     Dim strSavedPIN As String = GetPINCookie() ' shows "123456"

     Response.Redirect("Page2.aspx")


Page 2 Load logic:


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strSavedPIN As String = GetPIN() ' my cookie is now blank
...


This is in a global module referenced by both pages:


    Public Sub SavePINCookie(ByVal v_strPIN As String)
        Dim MCSISOCookie As HttpCookie
        MCSISOCookie = New HttpCookie("MCSISOPIN")

        With MCSISOCookie
            .Expires = DateAdd(DateInterval.Minute, 60, Now)
            .Value = v_strPIN
        End With

        HttpContext.Current.Request.Cookies.Add(MCSISOCook ie)

        MCSISOCookie = Nothing
    End Sub

    Public Function GetPINCookie() As String
        GetPINCookie = ""

        Dim MCSISOCookie As HttpCookie
        MCSISOCookie = HttpContext.Current.Request.Cookies.Get("MCSISOPIN ")

        If Not (MCSISOCookie Is Nothing) Then
            GetPINCookie = MCSISOCookie.Value
        End If

        MCSISOCookie = Nothing
End Sub


Thanks for looking at it.
  #4 (permalink)  
Old November 8th, 2007, 05:08 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Never mind - I figured this one out for myself finally.

For inquiring minds who want to know, there is a difference between .REQUEST.COOKIES and .RESPONSE.COOKIES. Why this difference exists I couldn't begin to guess, especially when it appears only to lead to confusion, but somebody in MS must've decided in their infinite wisdom that there was a need for two sets of cookies anyway.

In any case, I changed the Save Cookie code to:


    Public Sub SavePINCookie(ByVal v_strPIN As String)
        Dim MCSISOCookie As HttpCookie
        MCSISOCookie = New HttpCookie("MCSISOPIN")

        With MCSISOCookie
            .Expires = DateAdd(DateInterval.Minute, 60, Now)
            .Value = v_strPIN
        End With



        ' CHANGED LINES

        HttpContext.Current.Response.Cookies.Add(MCSISOCoo kie)
        'HttpContext.Current.Request.Cookies.Add(MCSISOCoo kie)



        MCSISOCookie = Nothing
    End Sub


If I am interpreting the way this works correctly, REQUEST.COOKIES are those sent from the server to the browser, whereas RESPONSE.COOKIES are those sent from the browser to the server. The distinction is important in my instance because page 1 needs to set RESPONSE.COOKIES before redirecting to page 2. REQUEST.COOKIES are NOT sent to the server and page 2 will therefore never get them. Page 2, otoh, has to get REQUEST.COOKIES because RESPONSE.COOKIES are what it may send if needed to page 3. However, I believe that if I wanted to check a RESPONSE.COOKIE in the same page in which it was set, I would get RESPONSE.COOKIE because it would not exist in REQUEST.COOKIE.

What a PITA! Thanks again, MicroSoft, for once again unnecessarily complicating what ought to be very simple.
  #5 (permalink)  
Old November 9th, 2007, 01:32 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I don't agree with your observations. IMO, the difference makes a lot of sense. The Request object is related to everything that deals with the request; e.g the request that the user makes for a page or other resource. The Request object contains a lot of useful info, like the request page, browser data and Cookies as they are related to the Request.

The Response, on the other hand, deals with the response from the server to the client. It contains the actual page output and cookies as they are related to the actual response back to the client.

So, there is a clear distinction between the two, as they happen at two locations / moments in the page life cycle.

I think this is one of the things that are hard to understand at first, but that you later learn to appreciate.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #6 (permalink)  
Old November 9th, 2007, 05:16 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

I believe my observations are correct, but my opinion is...well, a matter of opinion. If your disagreement with my opinion can be a stepping off point for discussing the value of both sets of cookies, I welcome the discussion as I think there is always something to be learned even when I disagree with others' opinions.

A single set of cookies makes more sense from my perspective, but then I haven't tried to do everything under the sun with them - I just wanted to pass a simple value between pages without using visible querystrings, hidden form fields, or unreliable session variables. Some decent documentation explaining when I would want to use one rather than the other would have saved me two days of trying to figure out the difference for myself, but I could find little information on line. Perhaps our discussion here will illuminate the differences for others who are likewise puzzled by two sets of cookies.

So, to start, can you provide an example of when I should want to set a REQUEST.COOKIE or get a RESPONSE.COOKIE?

In an unrelated thread to those who manage P2P, I do not seem to be receiving email when there are responses to this thread or most others I subscribe to.
  #7 (permalink)  
Old November 9th, 2007, 05:50 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by Ron Howerton


So, to start, can you provide an example of when I should want to set a REQUEST.COOKIE or get a RESPONSE.COOKIE?
Sure.

Using Request.Cookie would allow you to rename a given cookie on the fly without much effort so something like:

HttpCookie c = Request.Cookies["foo"];
Request.Cookies.Remove("foo");
c.Name = "MyNewCookie";
Request.Cookies.Add(c);

This, of course, assumes that a cookie exists within the request object named 'foo'.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
  #8 (permalink)  
Old November 10th, 2007, 07:24 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I had no intention to start a flame war about observations or opinion. However, it felt a bit awkward that you are angry with Microsoft, simply because you ran into something that isn't intuitive or easy to discover in the first place. Also this:
Quote:
quote:REQUEST.COOKIES are those sent from the server to the browser, whereas RESPONSE.COOKIES are those sent from the browser to the server.
seems like an incorrect observation to me anyhow ;)
Cookies in the Request are associated with the request the client makes to the server, and are thus sent from the client to the server, and not the other way around. The same goes for the Response cookies.

I think the biggest reason for two collections may be performance. If you have a lot of read-only cookies that are only set once, there's no need to send them back with every response. With two separate collections (which have elements that can refer to the same cookie to make things even more confusing) only those that are new or modified need to be sent to the browser.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #9 (permalink)  
Old November 13th, 2007, 09:36 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Expressing my opinion about MS's appeasement of the gods of OOP and the resulting mayhem their unecessary obfuscation has caused is not the basis of a flame war, IMHO. It certainly wasn't intended as an attack on you Omar, whatever your relationship to MS, and it's not like I'm the first developer to have cursed MS, either. In any case, I appreciate the help I receive on P2P and apologize if you interpreted it as anything other than an anguished cry for help in the .NET wilderness.

As for the statement referenced below, it pretty much reads the same as one of the few sites I could find that discussed the subject:

http://www.thescripts.com/forum/thread328043.html

Whether this site is in error or not, it sent me exploring in a direction that cleared up my problem, anyway. But if it is in error, all the more reason for discussing it further and reducing the misinformation on the subject. MS's own site is virtually worthless on the subject, and the apparent proliferation of misinformation elsewhere only makes it all the more confusing. Can you point me to a source that actually discusses the differences between the two cookie objects and provides something more than a rudimentary example of setting/getting a simple cookie?

And thanks to DParsons for providing an example as requested, although I'm still not sure I understand when I would want to set/get one rather than the other. Like I said before, I have not tried to do everything possible with cookies - I just wanted to consistently set and get values and it did not appear to work as advertised according to any sites I could find.

As for my other observation, if anybody cares, I received an email about DParson's contribution to the thread but not Imar's. Very curious!
  #10 (permalink)  
Old November 13th, 2007, 09:51 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Don't worry. I don't take it personally. I am not related to Microsoft other than that I use their products and write about them from time to time. I would have given you the same response if you had talked about Orcale's 8i or Black & Decker's FireStorm 8.5A

I completely understand your "cry for help" and hopefully I have given you that impression right from the start of this thread (and previous posts as well)

Anyway, no, no other links to point you too, unfortunately. I agree that it's difficult to get good information and thorough explanations on this subject.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie prv299 ASP.NET 2.0 Professional 3 May 29th, 2008 11:26 AM
cookie turn off swagatika ASP.NET 1.0 and 1.1 Basics 2 March 15th, 2006 09:25 AM
Cookie Handling mahulda General .NET 1 August 23rd, 2004 06:58 AM
Cookie deleting value andybeh ASP.NET 1.0 and 1.1 Basics 0 June 4th, 2004 12:24 AM
login cookie help daddycool2k ASP.NET 1.0 and 1.1 Basics 0 November 16th, 2003 03:17 PM





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