.Net Uri 编码 RFC 2396 与 RFC 3986
首先,一些简单的背景知识...作为与第三方供应商集成的一部分,我有一个 C# .Net Web 应用程序,它接收一个 URL,其中包含查询字符串中的大量信息。该 URL 使用 MD5 哈希值和共享密钥进行签名。基本上,我提取查询字符串,删除它们的散列,对剩余的查询字符串执行我自己的散列,并确保我的散列与提供的匹配。
我正在按以下方式检索 Uri...
Uri uriFromVendor = new Uri(Request.Url.ToString());
string queryFromVendor = uriFromVendor.Query.Substring(1); //Substring to remove question mark
我的问题源于包含特殊字符(例如元音变音 (ü))的查询字符串。供应商根据 RFC 2396 表示(%FC
)计算其哈希值。我的 C# .Net 应用程序根据 RFC 3986 表示(%C3%BC
)计算其哈希值。不用说,我们的哈希值不匹配,我抛出了我的错误。
奇怪的是,.Net 中 Uri 类的文档说它应该遵循 RFC 2396,除非另外设置为 RFC 3986,但我的 web.config
文件中没有他们所说的此行为所需的条目。
如何强制 Uri 构造函数使用 RFC 2396 约定?
如果做不到这一点,是否有一种简单的方法将 RFC 3986 八位位组对转换为 RFC 2396 八位位组?
First, some quick background... As part of an integration with a third party vendor, I have a C# .Net web application that receives a URL with a bunch of information in the query string. That URL is signed with an MD5 hash and a shared secret key. Basically, I pull in the query string, remove their hash, perform my own hash on the remaining query string, and make sure mine matches the one that was supplied.
I'm retrieving the Uri in the following way...
Uri uriFromVendor = new Uri(Request.Url.ToString());
string queryFromVendor = uriFromVendor.Query.Substring(1); //Substring to remove question mark
My issue is stemming from query strings that contain special characters like an umlaut (ü). The vendor is calculating their hash based on the RFC 2396 representation which is %FC
. My C# .Net app is calculating it's hash based on the RFC 3986 representation which is %C3%BC
. Needless to say, our hashes don't match, and I throw my errors.
Strangely, the documentation for the Uri class in .Net says that it should follow RFC 2396 unless otherwise set to RFC 3986, but I don't have the entry in my web.config
file that they say is required for this behavior.
How can I force the Uri constructor to use the RFC 2396 convention?
Failing that, is there an easy way to convert the RFC 3986 octet pairs to RFC 2396 octets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
+1 阁楼!我回去尝试删除我正在创建的无关 Uri,突然,该字符串的元音变音编码为 UTF-8 而不是 UTF-16。
起初,我认为这行不通。在此过程中,我尝试使用
Request.QueryString
检索 url,但这导致元音变音显示为%ufffd
,即 � 字符。为了采取新的视角,我尝试了阿提卡的建议,它奏效了。我很确定答案与 我在这里读到的东西。
我的问题源于这里...
通过获取
Request.Url
uri 并创建另一个 uri,它被编码为 C# 标准 UTF-16。通过使用原始的uri,它保留在.Net标准UTF-8中。感谢大家的帮助。
+1 for atticae! I went back to try removing the extraneous Uri I was creating and suddenly, the string had the umlaut encoded as UTF-8 instead of UTF-16.
At first, I didn't think this would work. Somewhere along the line, I had tried retrieving the url using
Request.QueryString
, but this was causing the umlaut to come through as%ufffd
which is the � character. In the interest of taking a fresh perspective, I tried atticae's suggestion and it worked.I'm pretty sure the answer has to do with something I read here.
My problems stemmed from here...
By taking the
Request.Url
uri and creating another uri, it was encoding as the C# standard UTF-16. By using the original uri, it remained in the .Net standard UTF-8.Thanks to all for your help.
我想知道这是否有点转移注意力:
我这样说是因为 FC 是带有变音符号的 u 的 UTF16 表示形式; C2BC 是 UTF8 表示形式。
我想知道将源数据转换为普通 .Net 字符串的 System.Text.Encoding 方法之一是否有帮助。
这个问题可能也很有趣:Encode and Decode rfc2396 URLs
I'm wondering if this is a bit of a red herring:
I say this because FC is the UTF16 representation of the u with umlaut; C2BC is the UTF8 representation.
I wonder if one of the System.Text.Encoding methods to convert the source data into a normal .Net string might help.
This question might be of interest too: Encode and Decode rfc2396 URLs
我不知道 Uri 构造函数的标准编码,但如果其他一切都失败,您始终可以自己解码 URL 并以您喜欢的任何编码对其进行编码。
HttpUtility 类 有一个
UrlDecode()
和UrlEncode()
方法,可让您指定 System.Text.Encoding作为第二个参数。例如:
I don't know about the standard encoding for Uri constructors, but if everything else fails you could always decode the URL yourself and encode it in whatever encoding you like.
The HttpUtility-Class has an
UrlDecode()
andUrlEncode()
method, which lets you specify the System.Text.Encoding as second parameter.For example: