Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. !!!Cookies!!!

!!!Cookies!!!

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin_Mannion
    wrote on last edited by
    #1

    Hi All, I thought I had finally figured out cookies and have now run into another problem. I have 2 pages which both add data to a cookie as follows **********Page 1********** HttpContext.Current.Response.Cookies["MyCookie"]["Session"]= Session.SessionID; HttpContext.Current.Response.Cookies["MyCookie"]["MyName"] = "Gavin"; HttpContext.Current.Response.Cookies["MyCookie"]["MyDrink"] = "Beer"; **********Page 2********** HttpContext.Current.Response.Cookies["MyCookie"]["MyFood"]= "Pizza"; HttpContext.Current.Response.Cookies["MyCookie"]["MyTeam"] = "Liverpool"; ************************** After Page 1 as I enter Page 2 my 3 Cookie Keys (Session, MyName & MyDrink) are all fine, but as I try and add the 2 new Keys my cookie loses the first 3? I hope everyone understood that one :) Can someone explain why this is and how do I stop it. I am looking at saving about 100 different things in this cookie so I don't really want to rewrite all the keys on every page. Thanks, Gavin

    J 1 Reply Last reply
    0
    • G Gavin_Mannion

      Hi All, I thought I had finally figured out cookies and have now run into another problem. I have 2 pages which both add data to a cookie as follows **********Page 1********** HttpContext.Current.Response.Cookies["MyCookie"]["Session"]= Session.SessionID; HttpContext.Current.Response.Cookies["MyCookie"]["MyName"] = "Gavin"; HttpContext.Current.Response.Cookies["MyCookie"]["MyDrink"] = "Beer"; **********Page 2********** HttpContext.Current.Response.Cookies["MyCookie"]["MyFood"]= "Pizza"; HttpContext.Current.Response.Cookies["MyCookie"]["MyTeam"] = "Liverpool"; ************************** After Page 1 as I enter Page 2 my 3 Cookie Keys (Session, MyName & MyDrink) are all fine, but as I try and add the 2 new Keys my cookie loses the first 3? I hope everyone understood that one :) Can someone explain why this is and how do I stop it. I am looking at saving about 100 different things in this cookie so I don't really want to rewrite all the keys on every page. Thanks, Gavin

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      It appears you're right, the cookies are reset after each round trip; but the more I thought about it the more it made sense. A cookie is just text that is passed back and forth between the server and the client. And if memory serves correctly to not pass the cookie back to the client tells the client to delete the cookie. One thing you can do to preserve the cookies is to loop through the list and re-add the cookie to the response. for( int i = 0; i < Request.Cookies.Count; i++ ) {   Response.Cookies.Add(Request.Cookies[i]); } This is something I think ASP did for you, but I don't remember off hand. Now, to save you from bickering from your users; PLEASE don't use cookies to store that much data :) Its okay to store a few bits, but you are limited in your cookie space by most browsers; and that is more traffic that goes to the server and back to the client. ASP.NET's session object is now webfarm friendly with use of either an out-of-proc server or using SQL Server as the session state holder. With that said you're going to need a way to store your data, if you can't use a database to store the data then at least put all your data in a memory stream (or some other form so it is one contigous block), compress it, then base64 encode it and store that in the cookie. The space you save will make your users and your provider happy :) Good Luck, James Simplicity Rules!

      G 1 Reply Last reply
      0
      • J James T Johnson

        It appears you're right, the cookies are reset after each round trip; but the more I thought about it the more it made sense. A cookie is just text that is passed back and forth between the server and the client. And if memory serves correctly to not pass the cookie back to the client tells the client to delete the cookie. One thing you can do to preserve the cookies is to loop through the list and re-add the cookie to the response. for( int i = 0; i < Request.Cookies.Count; i++ ) {   Response.Cookies.Add(Request.Cookies[i]); } This is something I think ASP did for you, but I don't remember off hand. Now, to save you from bickering from your users; PLEASE don't use cookies to store that much data :) Its okay to store a few bits, but you are limited in your cookie space by most browsers; and that is more traffic that goes to the server and back to the client. ASP.NET's session object is now webfarm friendly with use of either an out-of-proc server or using SQL Server as the session state holder. With that said you're going to need a way to store your data, if you can't use a database to store the data then at least put all your data in a memory stream (or some other form so it is one contigous block), compress it, then base64 encode it and store that in the cookie. The space you save will make your users and your provider happy :) Good Luck, James Simplicity Rules!

        G Offline
        G Offline
        Gavin_Mannion
        wrote on last edited by
        #3

        Okay in original ASp I was told to never store so much data in a Session Object and to either use Cookies or a Database. Now I'm being told not to use Cookies for this much data? You just can't win, at least it makes sense now though, I will use my SQL database instead. Cheers, Gavin PS: Thanks for the help :-D :-D :-D

        J 1 Reply Last reply
        0
        • G Gavin_Mannion

          Okay in original ASp I was told to never store so much data in a Session Object and to either use Cookies or a Database. Now I'm being told not to use Cookies for this much data? You just can't win, at least it makes sense now though, I will use my SQL database instead. Cheers, Gavin PS: Thanks for the help :-D :-D :-D

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Original ASP had the problem where the session object wasn't webfarm friendly, so you had major scalability issues involved. With ASP.NET you can use a SQL Server Database to store the session data or an out-of-proc server (exe) on the network to host it; so it is webfarm friendly and thus scales pretty well. Now of course session data is going to take up RAM on the server the session is stored on; but how much data is really being taken up per session? Probably not enough to make it matter. James Simplicity Rules!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups