title: Handling encoded cookie values in C#
body: <p>According to <a href="http://www.ietf.org/rfc/rfc2109.txt" rel="nofollow noreferrer">RFC 2109</a>, the value of a cookie "is opaque to the user agent and may be anything the origin server chooses to send, possibly in a server-selected printable ASCII encoding."</p>
  
  <p>As a consequence of this, different languages/platforms/server send a different cookie value even when the original value is the same.</p>
  
  <p>For example, C#/ASP.NET send the text as is; classic ASP urlencodes and urldecodes the text; Perl/Apache urlencodes/decods the text (but differently from ASP!). Php gives you the option.</p>
  
  <p>I am writing a Single Sign-On system that needs to share a cookie with wildly different applications. In particular I have .NET, Java, Perl, ColdFusion that need to be supported out-of-the-box.</p>
  
  <p>The text I store in the cookie is always a valid <a href="http://www.neurophys.wisc.edu/comp/docs/ascii/#table" rel="nofollow noreferrer">ASCII-7</a> string. Nevertheless, Perl likes to encode some 7-bit ASCII characters, for example.</p>
  
  <p>I see two main alternatives to make this work:</p>
  
  <ol>
  <li><p>Only accept non-encoded values. After all there is no need to encode them. This is how it is at the moment. Obviously all integrated systems must be able to support non-encoded values.</p></li>
  <li><p>Accept both encoded and non-encoded values. This would allow for maximum compatibility out of the box, but I would need to determine whether a particular value is encoded or not (which sounds quite impossible: is "%20" the literal "%20" string or a space?)</p></li>
  </ol>
  
  <p><strong>Which solution would you suggest and why? If it is #2, how would you detect UrlEncoded text?</strong></p>
  
  <hr>
  
  <p>Example of a cookie (I've added line breaks to make it fit)</p>
  
  <pre><code>A5A2794D694241AD92F9B22F288EFAA1|8428DCCC|20090821142732|20090821142832|
  10.100.107.40|955098D50AB4982D4E247EFA53F4E23B32A05ED0131E096709BE1D8CCC
  8A3CA18252D376473C244FD71C462AB42CF54C
  </code></pre>