title: Unable to decode Base-64 URLs
body: <p>I have an application that builds an HTML email.  Included in the content is an encoded URL parameter that might, for example, contain a promotional code or product reference.  The email is generated by a Windows service (essentially a console application) and the link, when clicked is handled by an MVC web site.  Here is the code for creating the email link:</p>
  
  <pre><code>string CreateLink(string domain, string code) {
      // code == "xyz123"
      string encrypted = DES3Crypto.Encrypt(code);  // H3uKbdyzrUo=
      string urlParam = encrypted.EncodeBase64();   // SDN1S2JkeXpyVW890
      return domain + "/" + urlParam;
  }
  </code></pre>
  
  <p>The action method on the MVC controller is constructed as follows:</p>
  
  <pre><code>public ActionResult Index(string id) {
      string decoded = id.DecodeBase64();
      string decrypted = DES3Crypto.Decrypt(decoded);
      ...
  }
  </code></pre>
  
  <p>In all our testing, this mechanism has worked as expected, however, now we have gone live we are seeing around a 4% error rate where the conversion from base-64 fails with the following exception:</p>
  
  <p><em>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.</em></p>
  
  <p>The id parameter from the url 'looks' OK.  The problem appears to be with the EncodeBase64/DecodeBase64 methods that are failing as DecodeBase64 method returns a 'garbled' string such as "�nl����□��7y�b�8�sJ���=" on the failed links.</p>
  
  <p>Furthermore, most of the errors are from IE6 user agents leading me to think this is a character encoding problem but I don't see why.</p>
  
  <p>For reference, here is the code for my base-64 URL encoding:</p>
  
  <pre><code>  public static string EncodeBase64(this string source)
    {
       byte[] bytes = Encoding.UTF8.GetBytes(source);
       string encodedString = HttpServerUtility.UrlTokenEncode(bytes);
       return encodedString;
    }
  
    public static string DecodeBase64(this string encodedString)
    {
       byte[] bytes = HttpServerUtility.UrlTokenDecode(encodedString);
       string decodedString = Encoding.UTF8.GetString(bytes);
       return decodedString;
    }
  </code></pre>
  
  <p>Any advice would be much appreciated.</p>