We help IT Professionals succeed at work.

Javascript encoding cookie to be read by asp.net in different encoding

lefodnes asked
I'm having difficulties understanding the encoding "circus"... Again.
I'm writing a cookie in javascript in a plain HTML file, like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
        document.cookie = "testCookie=TestingÆØÅ";


Open in new window

Then I'm trying to read this into a string in a .ashx file in the same directory:
<%@ WebHandler Language="C#" Class="readcookie" %>

using System;
using System.Web;

public class readcookie : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        HttpCookie hc = context.Request.Cookies["testCookie"];
        if (hc != null)
            string s = hc.Value;
    public bool IsReusable {
        get {
            return false;


Open in new window

Running that ashx-file gives me

Open in new window

I don't see the ÆØÅ

Checking the Request Headers in the debugging panel in Google Chrome for the ashx file, I see this:
GET /readcookie.ashx HTTP/1.1
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2
Cookie: testCookie=TestingÆØÅ

Open in new window

No ÆØÅ there either...
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

I don't know what the solution is but you have not declared the character encoding in any of those files.  UTF8 is the most common recommendation these days but it doesn't necessarily display correctly in your editor... if your editor is not using UTF8.  Firefox says the character encoding on this page is Unicode which usually means UTF8.  Line 4 on this page is:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window


This page on experts-exchange should not interfere, since it is displaying the characters as I see them in my browser and editors (visually).
I tried adding <meta charset="UTF-8" /> in the .html file that sets the cookie, but there was the same result. All files are saved as utf-8 encoding. I think IIS looks at this when serving the files to the browser. I'm suspecting that the javascript and the browser is running UCS-2 or UTF-16, and maybe I should convert in asp.net, but I would like to have an expert's opinion on this. What would be the recommended way of doing this?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

This page on experts-exchange should not interfere
That is a misconception that I have seen here quite a number of times.  The character set encoding of this page determines the character set used by ALL text on this page.  People have complained that their posts don't look like what they thought they posted and that is always the reason why.

One of the biggest reasons that character encoding is a problem is that changing the declaration does not change the character codes.  The character set declaration is there to tell the browser what character set to use in displaying the text... using the character codes that are in the page text.

Here is the Microsoft page on the subject:  https://msdn.microsoft.com/en-us/library/39d1w2xf%28v=vs.140%29.aspx


That is a misconception...
Well. I'm just saying it doesn't interfere with the .html and .ashx files I am running on my local IIS.

I'm guessing we're both seeing the same thing on this EE page, unless one of us have a totally messed up browser. It is declaring UTF-8, and both our browsers are using that encoding.

As I wrote, on my machine, it looks the same on the EE Firefox output and the local file Chrome output.
This is my ashx file: Screen shot of ashx output.. and this is the corresponding Chrome headers output
Screen shot of headers sectionI also changed the ashx like this:
context.Response.ContentType = "text/plain; charset=UTF-8";

Open in new window

and the .html like this:
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

Unfortunately, it is the same result.
Fixer of Problems
Most Valuable Expert 2014
I'm seeing the same thing that you show in your screen shots.  If you click on the Cookies tab, is the display the same?  

Looks like IIS and/or ASP.NET are altering it somehow.  I assume that ASCII characters don't get changed?

It appears that your characters in your cookie value should be encoded with encodeURI() since they are not all ASCII characters.  http://www.w3schools.com/tags/ref_urlencode.asp
Original Cookie spec: http://curl.haxx.se/rfc/cookie_spec.html
Current RFC : http://tools.ietf.org/html/rfc6265


Took a while before I got to test this, but now I did. It solved the problem, and now all letters are correctly read by the server side.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Good, glad it's working.!