How do cookies work in ASP.NET?

The website I work for consists of several projects (written in several languages). Now we need to use some inconvenient code in the query strings and session variables so that the person logs in when they move from project to project. Since cookies are domain-specific, we try to convert them, because they can be installed in one project using one language, but another project (in the same domain) can use it in a different language.

However, I am having problems changing the value of the cookie and deleting it. Or, to be more specific, I am having trouble making any changes to the cookie stick.

For example, in my exit code:

if (Request.Cookies["thisuserlogin"] != null) { HttpCookie myCookie = new HttpCookie("thisuserlogin"); myCookie.Value = String.Empty; myCookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(myCookie); Response.Cookies.Set(myCookie); litTest.Text = myCookie.Expires.ToString() + "<br />" + Request.Cookies["thisuserlogin"].Expires.ToString(); } 

I finish with one line yesterday and the next line is 1/1/0001 12:00:00, although they MUST be the same cookie. So why, although a cookie was set, this value has not changed? Is there a way to get the user's computer to update the cookie value, including deletion?

Many thanks. PS Any URLs you can provide to give an easy-to-understand tutorial for cookies will be appreciated.

+6
source share
2 answers

http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx

 if (Request.Cookies["thisuserlogin"] != null) { HttpCookie byeCookie = new HttpCookie("thisuserlogin"); byeCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(byeCookie); // Update Client Response.Redirect(Request.RawUrl); } 
+1
source

On the client side, you should use the Fiddler tool to capture all the data going back and forth. This will help you see that your cookie should be set with a date in the past (and also not be present in the next request).

Regarding the output of your text field, you publish a cookie in which you created the expiration time and the expiration time of the cookie request , which does not exist. If instead you should see the cookie response, you should see the set date. In addition, a call to Response.Cookies.Set not needed. Response.Cookies.Add should be all you need.

0
source

Source: https://habr.com/ru/post/924581/


All Articles