0

In a .net MVC project we're using serialized (to JSON) classes between requests for persistence. Basically we store serialized userdata within cookies so that we can use it again in later stages and render it on certain places.

One of the items we serialize is the user's location of residence, which in some cases contains a special character like the 'ë' (instead of a normal 'e'). After serializing this character, the cookie contains a sequence that is supposed to represent the character, though when we render the string on the screen it does not format it again the way it is supposed to.

Example:

  • Input string: Sint Odiliënberg
  • String in cookie: Sint Odiliënberg
  • String when rendered: Sint Odili%C3%83%C2%ABnberg

Obviously I need the rendered string to be identical to the input string.

Code we use to serialize (json.net library):

public class ClassWithResidenceProperty {
    public string Residence {get;set;}
    public string Serialize()
    {
        return JsonConvert.SerializeObject( this );
    }
}

Also, I cannot reproduce this bug when I run the application locally. It makes me think that there might be some culture flag I need to set somewhere. I tried this:

return JsonConvert.SerializeObject( this, 
    new JsonSerializerSettings() 
    { 
         Culture = new CultureInfo("nl-NL")
    }
);

(I live in the Netherlands). However, forcing this culture does not change anything on our test environment.

  • I'm looking for an idea of what could be causing this issue.
  • What I essentially need is the string rendered in the correct format.

Any suggestion or remark is appreciated.

Glubus
  • 2,819
  • 1
  • 12
  • 26
  • 1
    What you're seeing there is the encoding required for HTTP HOST entries. I don't know the decoding method of the top of my head, but I believe it's something like 'HttpUtility.UrlDecode(...)` in `System.Web`. – Patrick Bell Aug 22 '16 at 15:05
  • 1
    The cookie encoding has it wrong already: "ë" is a garbled representation of the e-trema unicode character mapped back to some ANSI code page... try using UTF-8 for cookie encoding: http://stackoverflow.com/questions/1969232/allowed-characters-in-cookies, http://stackoverflow.com/questions/25665703/encoding-scheme-used-for-cookies – Cee McSharpface Aug 22 '16 at 15:07
  • Those answers suggest that Chrome encodes cookies with UTF-8 by default though... Any idea where I would define this? – Glubus Aug 23 '16 at 08:05

0 Answers0