Thread: Anyone here well versed in ASP.NET? I have some questions about Cookies and Sessions

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    Anyone here well versed in ASP.NET? I have some questions about Cookies and Sessions

    I know that cookies are messages passed back and forth between client and server that the server uses to associate a particular user to a session, and that a session is a store on the server that contains data that the server uses to restore the state of pages between get requests.

    But implementing this functionality is a bit tricker than I imagined. Consider the following event handler procedure, which throws an ArgumentOutOfRangeException when the button is clicked:

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write(this.Request.Cookies[0].Value + "<br/>");//ArgumentOutOfRangeException
            }
    Not even a single cookie. And note that Response.Cookies[0] would have caused the same exception.

    Now consider the following sample code, which is pretty much the same as the previous code but with an extra line of code:

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write(this.Session.SessionID + "<br/>");
                Response.Write(this.Request.Cookies[0].Value + "<br/>");
            }
    The simple act of accessing the SessionID property of the HttpSessionState object returned by the Page's Session property fixes the problem. Now the HttpCookieCollection object returned by the HttpRequest's (or HttpResponse's) Cookies property has at least one element.

    Each time we click on the button we should see something like this on the page, and each time it would be a new sessionID:

    cvbuioycvbouiyuisriieerhk
    cvbuioycvbouiyuisriieerhk

    I interpret this to mean that some code inside HttpSessionState's SessionID's get accessor adds a key/value pair to the HttpCookieCollections associated with the HttpRequest and HttpResponse objects. Who would have thought it?

    This would also have worked fine:

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write(this.Session.SessionID + "<br/>");
                Response.Write(this.Response.Cookies[0].Value + "<br/>");
            }
    And now, if we want to make the session ID remain the same between postbacks, the following line code appears to solve that problem:

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["..."] = "";
                Response.Write(this.Session.SessionID + "<br/>");
                Response.Write(this.Request.Cookies[0].Value + "<br/>");
            }
    In other words, saving something to the HttpSessionState collection is all we need to do in order to make the SessionID be persisted between requests. Who would have thought it?

    But that's not all. Had we had this code, there would have been another ArgumentOutOfRangeException:

    Code:
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["..."] = "";
                Response.Write(this.Session.SessionID + "<br/>");
                Response.Write(this.Response.Cookies[0].Value + "<br/>");//ArgumentOutOfRangeException
            }
    However, if we remove the line of code that adds a value to the HttpSessionState collection, then Response.Cookies[0] gets populated with a new sessionID and the code runs fine.

    The bottom line is that I don't find any of this logic overly obvious and to be honest I feel like I'm wasting my time trying to learn all this through trial and error.

    Can someone help me make sense of all the code above and perhaps point me toward a resource where the rationale behind all this logic is clearly explained?
    Last edited by y99q; 03-13-2012 at 09:07 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm guessing you've read ASP.NET Session State? How is your config.web set up?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    71
    well I didn't really find a lot of new info in that article. as for my web.config, I'm using default settings. ie: sessionState mode = "InProc", etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SSL... too many tcp sessions...
    By ccie15672 in forum Networking/Device Communication
    Replies: 20
    Last Post: 05-22-2008, 07:44 PM
  2. cookies & cgi & c/c++
    By Victor in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-28-2004, 10:19 PM
  3. PHP and Cookies
    By Jperensky in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-15-2002, 12:10 PM
  4. Cookies
    By CodeMonkey in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 02-28-2002, 06:34 PM