HttpServerUtility.UrlTokenDecode does not work "sometimes" (invalid length for the Base-64 char array).

We have an IHttpHandler for style sheets and add the <link/> to the page as follows:

 HtmlGenericControl link = new HtmlGenericControl("link"); link.Attributes.Add("rel", "stylesheet"); link.Attributes.Add("href", "stylesheet.axd?d=" + HttpServerUtility.UrlTokenEncode(token)); head.Controls.Add(link); 

In the stylesheet.axd handler, we UrlTokenDecode the query string parameter d as follows:

 var token = HttpServerUtility.UrlTokenDecode(Request.QueryString["d"]); 

This works most of the time, but from time to time we find one of the following two exceptions in our journal:

 [FormatException: Invalid character in a Base-64 string.] System.Convert.FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length) System.Web.HttpServerUtility.UrlTokenDecode(String input) ... [FormatException: Invalid length for a Base-64 char array.] System.Convert.FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length) System.Web.HttpServerUtility.UrlTokenDecode(String input) System.Web.HttpServerUtilityWrapper.UrlTokenDecode(String input) ... 

Any ideas what might cause this phenomenon?

Notes:

  • the resulting URL is <1500, so below any known URL limits (e.g. IE: 2083).
  • seems to be independent of the user agent (we have these exceptions for IE6,7,8, FF and Chrome)
  • our (unconfirmed) suspicions include AntiVirus products, HTTP proxies, browser add-ons
  • found this question related to remoteness, but it concerns a problem with presentation in a view
+1
c # base64
Sep 08 '09 at 9:31
source share
1 answer

We had similar problems, so we avoided UrlTokenEncode, the reason is that base64 sometimes pads trailing characters like "==", etc., so they don’t get the correct transmission in the url. And I think there is a bug in .NET, however we changed it to the next and started working.

We did this, and it always worked.

 url += "&d=" + HttpServerUtility.UrlEncode( Convert.ToBase64String( ... )) 

and on the receiving side we only use

 data = Convert.FromBase64String( Request.QueryString["d"] ) 

We do not need to use UrlDecode, because the query string object stores information in a decoded format already.

0
08 Sep '09 at 9:45
source



All Articles