Cookies and Unicode characters

Sep 9, 2007

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

Comments (13) -

.:: m3rLinEz ::.
.:: m3rLinEz ::. United States
9/9/2007 2:19:51 PM #

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

Jimmi Bram Nielsen
Jimmi Bram Nielsen Denmark
9/10/2007 12:18:33 PM #

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

Mads Kristensen
Mads Kristensen Denmark
9/10/2007 12:22:38 PM #

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

Dan Atkinson
Dan Atkinson United Kingdom
9/10/2007 2:05:38 PM #

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. :-(

Mads Kristensen
Mads Kristensen Denmark
9/10/2007 4:15:39 PM #

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.

Jimmi Bram Nielsen
Jimmi Bram Nielsen Denmark
9/14/2007 5:33:38 PM #

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

MK2
MK2 People's Republic of China
9/19/2007 11:48:11 AM #

Nice tips.

Tom Clancy
Tom Clancy United States
11/14/2007 10:54:40 AM #

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

meghanad chitre
meghanad chitre India
11/23/2007 8:50:10 AM #

Thanks a bunch for this!

hangy
hangy Germany
1/23/2008 6:53:09 AM #

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 ...)

vijayakanth
vijayakanth India
5/21/2008 6:47:06 AM #

It fixes my problems too.

fin
fin
12/1/2008 4:01:57 AM #

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!

Reza Shirazi
Reza Shirazi
12/20/2008 10:27:47 AM #

Thank you so much. It fixes my problems too!

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.