Get the App
SLTechnology News&Howtos  ›  Development  › 

Why can't Response.Cookies.Remove delete COOKIE in ASP.NET

Shulou Source: shulou.com Published: 2022-06-03 06:17:18 09月20日 Update

This article mainly introduces why Response.Cookies.Remove in ASP.NET can not delete COOKIE, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Examples are as follows:

Protected void Page_Load (object sender, EventArgs e)

{

If (! IsPostBack)

{

HttpCookie UserInfo = new HttpCookie ("UserInfo")

UserInfo.Value = "bdstjk"

Response.Cookies.Add (UserInfo)

}

}

Protected void btnRemoveCookie_Click (object sender, EventArgs e)

{

Response.Cookies.Remove ("UserInfo")

Response.Write ("alert (\" Cookie deleted successfully! \ ");")

}

Protected void btnCheckCookie_Click (object sender, EventArgs e)

{

If (Request.Cookies ["UserInfo"]! = null)

{

Response.Write ("Cookie exists," + Request.Cookies ["UserInfo"] .value)

}

Else

{

Response.Write ("Cookie does not exist")

}

}

Page code:

The copy code is as follows:

Run the code test and you will find that cookie exists no matter how to click the delete button, as shown below:

Why is that? It is obvious that the operation of deleting cookie has been performed, why can't it be deleted?

Let's take a look at the source code of the HttpCookieCollection implementation of .NET.

The copy code is as follows:

Public void Remove (string name)

{

If (this._response! = null)

{

This._response.BeforeCookieCollectionChange ()

}

This.RemoveCookie (name)

If (this._response! = null)

{

This._response.OnCookieCollectionChange ()

}

}

This operation removes the cookie from the HttpCookieCollection collection, and when the server transfers the data to the client, it does not contain any information about the Cookie that has been deleted on the server, and the browser will not make any changes to it (the remove method simply does not allow the server to send the deleted cookie to the client, regardless of whether the cookie remains in the client or not). So the situation that the cookie can't be deleted appears.

So what should we do if we want to delete cookie?

Change the code to delete cookie to the following statement:

The copy code is as follows:

If (Request.Cookies ["UserInfo"]! = null)

{

Response.Cookies ["UserInfo"] .Expires = DateTime.Now.AddDays (- 1)

}

Response.Write ("alert (\" Cookie deleted successfully! \ ");")

Let's run the program again and test:

Okay. Cookie has been deleted. Force Cookie to expire by setting the expiration time of Cookie to negative. We can achieve the effect we need.

Since Response.Cookies.Remove has no way to achieve the effect we need, why does Microsoft keep it? because CookieCollection implements the ICollection interface, romove is a must, although it doesn't have much practical value. And the collection of romove should also be implemented in this way, but Microsoft in writing MSDN, the description is too unclear, causing us a lot of trouble.

Thank you for reading this article carefully. I hope the article "Why Response.Cookies.Remove can not delete COOKIE in ASP.NET" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Code article customer service success value client effect method time server Microsoft test run clear example interest method just just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei vpn NVidia Shulou Technology