miércoles, 23 de septiembre de 2015

Labeled as:

Duplicate cookies or that do not delete

Intended for: C# developers of all levels

Today I have come across with one of those "weirdness" that I love, the kind that make you lose midmorning and give you a hard time... Well, like those we must endure, we programmers, on a daily basis.

It turns out that the "cookies" that are used in 99% or maybe 100% of current web pages (well, maybe this information is pull out of the sleeve, nevertheless very much used) they are a pain in the ass. And not because in itself involve a special difficulty, no. It is for a bug that appears to exist in the enumeration Request.Cookies yResponse.Cookies.

How to detect when we are making a mistake? Well, the first symptom is the sight of more than one cookie with the same name in the lists above, that means, if we analyze the content of Request.Cookies or deResponse.Cookies and we detect in AllKeys property two or more times the same name, means that we are literally "screwed up".

Search in your code something like the following: this.Request.Cookies ["nameofyourCookie"] == null  or this.Response.Cookies ["nameofyourCookie"] == null.

And now I tell you that this is not working. And why? - you wonder. By the time you call the indexer this.Response.Cookies ["nameofyourCookie"] and without warning you create a new blank cookie (no data) with the same name.

So how I can delete a cookie since this would happens? I will illustrate it below with a practical example:

Code who fails:

if (this.Response.Cookies.[cookieName] != null)
{
      this.Response.Cookies.Remove(cookieName);
}

Code that works:

if (this.Response.Cookies.AllKeys.Contains(cookieName))
{
       this.Response.Cookies.Remove(cookieName);
}

This knowledge can be applied to other areas of cookies, just be careful to use the property AllKeys to check the existence of the cookie and it is more than enough.

I hope I've been helpful!

Unknown

Author y editor

0 comentarios:

Publicar un comentario

 

Friendly Websites:

  • Manuel Enrique Díaz Rodríguez
  • CuRadio
  • Copyright © DotNet Pathways 2015
    Distributed By My Blogger Themes | Designed By Templateism