Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report