.Net Uri Encoding RFC 2396 vs RFC 3986

Firstly, some quick background ... As part of the integration with a third-party provider, I have a C # .Net web application that gets a URL with a bunch of information in the query string. This URL is signed with an MD5 hash and a shared secret key. Basically, I pull in the query string, delete the hash, execute my own hash on the remaining query string and make sure mine matches the one that was provided.

I get Uri as follows ...

Uri uriFromVendor = new Uri(Request.Url.ToString()); string queryFromVendor = uriFromVendor.Query.Substring(1); //Substring to remove question mark 

My problem is with query strings that contain special characters like umlaut (ΓΌ). The provider calculates its hash based on the RFC 2396 view, which is %FC . My C # .Net application computes a hash based on the RFC 3986 view, which is %C3%BC . Needless to say, our hashes do not match, and I throw my mistakes.

Oddly enough, the documentation for the Uri class in .Net says that it should follow RFC 2396, unless otherwise specified in RFC 3986, but I do not. I have an entry in my web.config , which they say is required for of this behavior.

How can I force the Uri constructor to use the RFC 2396 convention?

Otherwise, is there an easy way to convert RFC 3986 octet pairs into RFC 2396 octets?

+8
uri rfc3986 url-encoding rfc2396
source share
3 answers

Nothing to do with your question, but why are you creating a new Uri here? You can simply do string queryFromVendor = Request.Url.Query.Substring(1) ; - atticae

+1 for the attic! I came back to try to remove the extraneous Uri that I created, and suddenly the line was umlaut, encoded as UTF-8 instead of UTF-16.

At first, I did not think it would work. Somewhere along the line, I tried to restore the URL using Request.QueryString , but it made the umlaut pass as %ufffd , which is a symbol .. In the interest of getting a fresh perspective, I tried to offer a suggestion, and it worked.

I am sure that the answer is related to what I read here .

C # uses UTF-16 in all of its lines, with coding tools when it comes to streams and files that lead us to ...

ASP.NET uses UTF-8 by default, and it's hard to think about a time when this is not a good choice ...

My problems arose here ...

 Uri uriFromVendor = new Uri(Request.Url.ToString()); 

By accepting Request.Url uri and creating another uri, it was encoded as the C # UTF-16 standard. Using the original uri, it remained in the .Net UTF-8 standard.

Thank you all for your help.

+2
source share

I am wondering if this is a little red herring:

I say this because FC is a UTF16 representation of u with an umlaut; C2BC is a representation of UTF8.

I wonder if one of the System.Text.Encoding methods can help to convert the source data to a normal string. Net.

This question may also be of interest: Encode and Decode rfc2396 URLs

+1
source share

I don’t know the standard encoding for Uri constructors, but if all else fails, you can always decode the URL yourself and encode it in whatever encoding you like.

HttpUtility-Class has an UrlDecode() and UrlEncode() method that allows you to specify System.Text.Encoding as the second parameter.

For example:

 string decodedQueryString = HttpUtility.UrlDecode(Request.Url.Query.Substring(1)); string encodedQueryString = HttpUtility.UrlEncode(decodedQueryString, System.Text.Encoding.GetEncoding("utf-16")); // calc hash here 
+1
source share

All Articles