Cookies and Unicode characters

by Mads Kristensen 9. September 2007 21:57

I’ve been having some issues with storing Unicode characters in cookies today. Whenever a cookie is set and the value filled with Unicode characters, the same characters cannot be retrieved from the cookie again. When they are retrieved from the requesting browser, they are changed into something totally unreadable.

Background

The cookie is set when a visitor enters some text into a textbox and submits the form. When the same visitor returns to that page I wanted to pre-fill the textbox with the value submitted earlier. Very easy and simple and not before someone noticed the strange behaviour with Unicode characters I thought it worked just fine.

Because the value was displayed in a textbox I thought that maybe HTML encoding could solve the issue. Don’t ever HTML encode a cookie in ASP.NET! It results in a yellow screen of death and an exception stating that the cookie contains dangerous characters. The dangerous character it was referring to was a HTML encoded representation of a Unicode character and looked something like this "#248;". The only thing to do is to delete your cookies in order to view that page again.

The solution

It took me a while to figure it out, but all you need to do is to URL encode the cookie value. It works no matter what encoding you use for the page. The example below illustrates the very simple solution:

private void SetCookie()
{
  HttpCookie cookie = new HttpCookie("cookiename");
  cookie.Expires = DateTime.Now.AddMonths(24);
  cookie.Values.Add("name", Server.UrlEncode(txtName.Text));
  Response.Cookies.Add(cookie);
}

private void GetCookie()
{
  HttpCookie cookie = Request.Cookies["cookiename"];
  if (cookie != null)
  {
    txtName.Text = Server.UrlDecode(cookie.Values["name"]);
  }
}

It is so simple but caused me a lot of time investigating and clearing cookies from the browser.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: ,

ASP.NET

Comments

9/9/2007 11:19:51 PM #

.:: m3rLinEz ::.

Wow! Nice solution! I have had the same problem with my blog (which is one of my early ASP.NET apps) but never tried to fix it. Thanks for the tips Smile

.:: m3rLinEz ::. United States |

9/10/2007 9:18:33 PM #

Jimmi Bram Nielsen

Thought you used to work at TraceWorks, where this problem was discovered and solved ages ago Smile

Jimmi Bram Nielsen Denmark |

9/10/2007 9:22:38 PM #

Mads Kristensen

Jimmi, I thought you work at Tracework, where you will know that it isn't used Smile

Mads Kristensen Denmark |

9/10/2007 11:05:38 PM #

Dan Atkinson

Hey Mads,

I see the localisation of your site changed to Danish.

Is there perhaps anything that could be checked at the user end, to change the language, based on the user's IP address? Or even perhaps, the ability to have an account, and from there, having their locale set?

My Danish is lacking greatly. :-(

Dan Atkinson United Kingdom |

9/11/2007 1:15:39 AM #

Mads Kristensen

Dan, it has been fixed. I was goofing around in the web.config yesterday and accidently removed the culture and uiculture attributes from the globalization element. It was set to "auto", but if it's missing it just uses the servers culture information, which in my case is Danish.

Mads Kristensen Denmark |

9/15/2007 2:33:38 AM #

Jimmi Bram Nielsen

Hey Mads, so I do, and we solved it for StatLynx version 1.0, our previous product Smile

Jimmi Bram Nielsen Denmark |

9/19/2007 8:48:11 PM #

MK2

Nice tips.

MK2 People's Republic of China |

11/14/2007 7:54:40 PM #

Tom Clancy

Thanks so much. Saved me tearing out my hair. Now that I see it, the answer was obvious all along.

Tom Clancy United States |

11/23/2007 5:50:10 PM #

meghanad chitre

Thanks a bunch for this!

meghanad chitre India |

1/23/2008 3:53:09 PM #

hangy

While searching the internet for a solution to this problem, I got to your site. Funny thing is that I have seen this solution on some different websites before, and someone there said that some "European characters" could not be UrlEncoded correctly. Did you ever experience anything like that or is it a non-issue in your opinion? Smile

What really annoyed me was that this cookie problem does not occur with my local development server (Cassini of VS2005) and thus was first seen on the production site. Duh. =) (Might also be connected to HTTP vs. HTTPS in this case, though ...)

hangy Germany |

5/21/2008 3:47:06 PM #

vijayakanth

It fixes my problems too.

vijayakanth India |

12/1/2008 1:01:57 PM #

fin

Glad to found this post!
I have the same problems with characters encoding with my cookies.
Searched the MSDN and came to a page on encoding, but it was complicated.
Your solution is simple and clean and fixed the problem well!
Thanks a lot!

fin |

12/20/2008 7:27:47 PM #

Reza Shirazi

Thank you so much. It fixes my problems too!

Reza Shirazi |

1/5/2009 9:21:29 PM #

pingback

Pingback from forum.ceviz.net

Cookie okumada dil problemi - Ceviz Forum

forum.ceviz.net |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008