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. Web Development
  3. ASP.NET
  4. question about cookies in c#

question about cookies in c#

Scheduled Pinned Locked Moved ASP.NET
questioncsharpdebugginghelp
5 Posts 3 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.
  • O Offline
    O Offline
    Old Gun
    wrote on last edited by
    #1

    I use cookies to pass values between,but there is a question about clear cookies,I have read a lot articles here,I can’t found where error is. This my code:

    		public void CreateCookies(string sCookiesName)
    		{
    			if (Request.Cookies[sCookiesName] == null)
    			{
    				HttpCookie cokExpr = new HttpCookie(sCookiesName);
    				DateTime dt = DateTime.Now;
    				TimeSpan ts = new TimeSpan(0,0,10,0,0);
    				cokExpr.Expires = dt + ts;
    				Request.Cookies.Add(cokExpr);
    				Response.Cookies[sCookiesName].Value = "";
    			}		
    		}
    		public string GetCookiesValue(string sCookiesName)
    		{
    			string sValue = "";
    			if (Request.Cookies[sCookiesName] != null)
    			{
    				sValue = Request.Cookies[sCookiesName].Value;
    			}
    			return sValue;
    		}
    		public void SetCookiesValue(string sCookiesName,string sValue)
    		{
    			if (Request.Cookies[sCookiesName] != null)
    			{
    				Response.Cookies[sCookiesName].Value = sValue;
    			}
    		}
    		public void RemoveCookies(string sCookiesName)
    		{
    			if (Request.Cookies[sCookiesName] != null)
    			{
    				DateTime dt = DateTime.Now;
    				TimeSpan ts = new TimeSpan(0,0,-10,0,0);
    				Response.Cookies[sCookiesName].Expires = dt + ts;
    				Request.Cookies.Remove(sCookiesName);
    			}
    		}
    

    The error is I clear cookies in page A, But I still can get my cookies in page B. I debug in page A,It seems my cookies cleard,this is why?

    A D 2 Replies Last reply
    0
    • O Old Gun

      I use cookies to pass values between,but there is a question about clear cookies,I have read a lot articles here,I can’t found where error is. This my code:

      		public void CreateCookies(string sCookiesName)
      		{
      			if (Request.Cookies[sCookiesName] == null)
      			{
      				HttpCookie cokExpr = new HttpCookie(sCookiesName);
      				DateTime dt = DateTime.Now;
      				TimeSpan ts = new TimeSpan(0,0,10,0,0);
      				cokExpr.Expires = dt + ts;
      				Request.Cookies.Add(cokExpr);
      				Response.Cookies[sCookiesName].Value = "";
      			}		
      		}
      		public string GetCookiesValue(string sCookiesName)
      		{
      			string sValue = "";
      			if (Request.Cookies[sCookiesName] != null)
      			{
      				sValue = Request.Cookies[sCookiesName].Value;
      			}
      			return sValue;
      		}
      		public void SetCookiesValue(string sCookiesName,string sValue)
      		{
      			if (Request.Cookies[sCookiesName] != null)
      			{
      				Response.Cookies[sCookiesName].Value = sValue;
      			}
      		}
      		public void RemoveCookies(string sCookiesName)
      		{
      			if (Request.Cookies[sCookiesName] != null)
      			{
      				DateTime dt = DateTime.Now;
      				TimeSpan ts = new TimeSpan(0,0,-10,0,0);
      				Response.Cookies[sCookiesName].Expires = dt + ts;
      				Request.Cookies.Remove(sCookiesName);
      			}
      		}
      

      The error is I clear cookies in page A, But I still can get my cookies in page B. I debug in page A,It seems my cookies cleard,this is why?

      A Offline
      A Offline
      Andy Brummer
      wrote on last edited by
      #2

      This is the best article on cookies under asp.net: http://www.codeproject.com/aspnet/aspnetcookies.asp[^] In trying to read a cookie creates it so Request.Cookies[sCookiesName] != null is always true.


      I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

      1 Reply Last reply
      0
      • O Old Gun

        I use cookies to pass values between,but there is a question about clear cookies,I have read a lot articles here,I can’t found where error is. This my code:

        		public void CreateCookies(string sCookiesName)
        		{
        			if (Request.Cookies[sCookiesName] == null)
        			{
        				HttpCookie cokExpr = new HttpCookie(sCookiesName);
        				DateTime dt = DateTime.Now;
        				TimeSpan ts = new TimeSpan(0,0,10,0,0);
        				cokExpr.Expires = dt + ts;
        				Request.Cookies.Add(cokExpr);
        				Response.Cookies[sCookiesName].Value = "";
        			}		
        		}
        		public string GetCookiesValue(string sCookiesName)
        		{
        			string sValue = "";
        			if (Request.Cookies[sCookiesName] != null)
        			{
        				sValue = Request.Cookies[sCookiesName].Value;
        			}
        			return sValue;
        		}
        		public void SetCookiesValue(string sCookiesName,string sValue)
        		{
        			if (Request.Cookies[sCookiesName] != null)
        			{
        				Response.Cookies[sCookiesName].Value = sValue;
        			}
        		}
        		public void RemoveCookies(string sCookiesName)
        		{
        			if (Request.Cookies[sCookiesName] != null)
        			{
        				DateTime dt = DateTime.Now;
        				TimeSpan ts = new TimeSpan(0,0,-10,0,0);
        				Response.Cookies[sCookiesName].Expires = dt + ts;
        				Request.Cookies.Remove(sCookiesName);
        			}
        		}
        

        The error is I clear cookies in page A, But I still can get my cookies in page B. I debug in page A,It seems my cookies cleard,this is why?

        D Offline
        D Offline
        Dario Solera
        wrote on last edited by
        #3

        I believe you're wroung about Request/Response objects. Request is the request received by the server from the client. When you add/remove items from Request, these will be not *saved* and sent back to the client. You have to modify only the Response object, that is the only thing that the client will receive. All the cookies, if not modified, will remain in the browser, and until expiration, they will be sent to the server on each request. So when you want to add a cookie: HttpCookie c = new HttpCookie(); // Add values to the cookie Response.Cookies.Add(c); When you want to remove a cookie: HttpCookie c = Request.Cookies["cookiename"]; c.Expires = DateTime.Now.AddDays(-10); Response.Cookies.Add(c); Briefing: Read cookies from Request AND REWRITE THEM TO Response if you want to modify or delete them. If not, they will remain in the browser. The Response is the only object that the server will *send* to the browser. NOTE: the only way to delete a cookie is to set its expiration date as a past date. I use this way in my websites and it works good.


        [ITA] Tozzi ha ragione: Gaia si sta liberando di noi. [ENG] Tozzi is right: Gaia is getting rid of us.

        O 1 Reply Last reply
        0
        • D Dario Solera

          I believe you're wroung about Request/Response objects. Request is the request received by the server from the client. When you add/remove items from Request, these will be not *saved* and sent back to the client. You have to modify only the Response object, that is the only thing that the client will receive. All the cookies, if not modified, will remain in the browser, and until expiration, they will be sent to the server on each request. So when you want to add a cookie: HttpCookie c = new HttpCookie(); // Add values to the cookie Response.Cookies.Add(c); When you want to remove a cookie: HttpCookie c = Request.Cookies["cookiename"]; c.Expires = DateTime.Now.AddDays(-10); Response.Cookies.Add(c); Briefing: Read cookies from Request AND REWRITE THEM TO Response if you want to modify or delete them. If not, they will remain in the browser. The Response is the only object that the server will *send* to the browser. NOTE: the only way to delete a cookie is to set its expiration date as a past date. I use this way in my websites and it works good.


          [ITA] Tozzi ha ragione: Gaia si sta liberando di noi. [ENG] Tozzi is right: Gaia is getting rid of us.

          O Offline
          O Offline
          Old Gun
          wrote on last edited by
          #4

          Thank you for your help.You are right.Will you give me a example how to pass values between two frames? My main page have 2 frames,Page A and B,when the value has changed in Page A,but I get old value in Page B,.

          D 1 Reply Last reply
          0
          • O Old Gun

            Thank you for your help.You are right.Will you give me a example how to pass values between two frames? My main page have 2 frames,Page A and B,when the value has changed in Page A,but I get old value in Page B,.

            D Offline
            D Offline
            Dario Solera
            wrote on last edited by
            #5

            A few months ago I was building an ASP.NET website, with the same problems: managing pages with 2+ frames. The biggest issue was to manage the refresh of the main page when one or more of its frames changed. So I had to use a odd mix between javascript, links with target=_parent, and some data passed via URL (Page.aspx?var=...). As a result the system was so complicated that I decided to trash it and rebuild in a smarter way: NO FRAMES. If this is not enough, you can think that the various browsers manage frames in different ways, so the behavior is not very predictable. No frames, hence, means that you have to write tons of code to assembly the page... However, the best way to pass values between 2 frames is to use links with the target attribute set to the correct frame, and use URLs to pass parameters. For example: Page M contains frame A and frame B. You want to tell to the frame A to write something on the page: In the page in the frame B: [Link](PageInFrameA.aspx?write=yourstring) In the page in the frame A the ASP code will perform the corretct operations to write the data. I hope this is useful... :)


            [ITA] Tozzi ha ragione: Gaia si sta liberando di noi. [ENG] Tozzi is right: Gaia is getting rid of us.

            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