question about cookies in c#
-
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?
-
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?
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
-
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?
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.
-
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.
-
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,.
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.