如何使用 HttpWebRequest 进行摘要身份验证?
WebRequest request = HttpWebRequest.Create(url);
var credentialCache = new CredentialCache();
credentialCache.Add(
new Uri(url), // request url
"Digest", // authentication type
new NetworkCredential("user", "password") // credentials
);
request.Credentials = credentialCache;
但是,这只适用于没有 URL 参数的 URL。例如,我可以很好地下载 http://example.com/test/xyz.html
,但是当我尝试下载 http://example.com/test?page= xyz
,结果是一条 400 Bad Request 消息,在服务器日志中包含以下内容(运行 Apache 2.2):
Digest: uri mismatch - </test> does not match request-uri </test?page=xyz>
我的第一个想法是摘要规范要求从摘要哈希中删除 URL 参数 - 但删除传递给 credentialCache.Add()
的 URL 参数没有改变任何事情。因此,情况肯定是相反的,.NET 框架中的某个地方错误地从 URL 中删除了参数。
Various articles (1, 2) I discovered make this look easy enough:
WebRequest request = HttpWebRequest.Create(url);
var credentialCache = new CredentialCache();
credentialCache.Add(
new Uri(url), // request url
"Digest", // authentication type
new NetworkCredential("user", "password") // credentials
);
request.Credentials = credentialCache;
However, this only works for URLs without URL parameters. For example, I can download http://example.com/test/xyz.html
just fine, but when I attempt to download http://example.com/test?page=xyz
, the result is a 400 Bad Request message with the following in the server's logs (running Apache 2.2):
Digest: uri mismatch - </test> does not match request-uri </test?page=xyz>
My first idea was that the digest specification requires URL parameters to be removed from the digest hash -- but removing the parameter from the URL passed to credentialCache.Add()
didn't change a thing. So it must be the other way around and somewhere in the .NET framework is wrongly removing the parameter from the URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从这篇文章中获取的代码对我来说非常有效 通过 HttpWebRequest 实现摘要身份验证C#
我遇到了以下问题,当我在浏览器中浏览 feed url 时,它会要求输入用户名和密码并且工作正常,但是上述任何代码示例在检查请求/响应标头(在 web 中)时都不起作用Firefox 中的开发人员工具)我可以看到标头具有摘要类型的授权。
步骤 1 添加:
}
调用新方法
步骤 2:如果 Url 为:http://xyz.rss.com/,则 文件夹/RSS
然后
域:http://xyz.rss.com(域部分)
dir:/folder/rss(url 的其余部分)
您也可以将其作为流返回并使用 XmlDocument Load() 方法。
Code taken from this post has worked perfectly for me Implement Digest authentication via HttpWebRequest in C#
I had following issue, when ever I browser the feed url in a browser it asked for username and password and worked fine, however any of the above code samples were not working, on inspecting Request/Response Header (in web developer tools in firefox) i could see header having Authorization of type digest.
Step 1 Add:
}
Step 2: Call new method
if Url is: http://xyz.rss.com/folder/rss
then
domain: http://xyz.rss.com (domain part)
dir: /folder/rss (rest of the url)
you could also return it as stream and use XmlDocument Load() method.
您说您删除了查询字符串参数,但是您是否尝试一直返回到主机?我见过的 CredentialsCache.Add() 的每个示例似乎仅使用主机,并且 CredentialsCache.Add() 将 Uri 参数列为“uriPrefix”,这似乎很能说明问题。
换句话说,尝试一下:
如果这有效,您还必须确保不会多次向缓存添加相同的“权限”...对同一主机的所有请求都应该能够使使用相同的凭证缓存条目。
You said you removed the querystring paramters, but did you try going all the way back to just the host? Every single example of CredentialsCache.Add() I've seen seems to use only the host, and the docs for CredentialsCache.Add() list the Uri parameter as "uriPrefix", which seems telling.
In other words, try this out:
If this works, you will also have to make sure that you don't add the same "authority" to the cache more than once... all requests to the same host should be able to make use of the same credential cache entry.
在早期的答案中,每个人都使用过时的 WEbREquest.Create 方法。
所以这是我的异步解决方案,是最近趋势的最新内容:
In earlier answers everybody use the obsolete WEbREquest.Create method.
So here is my async solution what up to date for recently trending's:
解决办法是在apache中激活这个参数:
更多信息:http://httpd.apache.org/docs/2.0/ mod/mod_auth_digest.html#msie
然后在 webrequest 对象的代码中添加此属性:
它对我来说非常有效
The solution is to activate this parameter in apache:
More info : http://httpd.apache.org/docs/2.0/mod/mod_auth_digest.html#msie
Then add this property in your code for the webrequest object:
it work very well for me
我认为第二个 URL 指向动态页面,您应该首先使用 GET 调用它来获取 HTML,然后下载它。虽然没有这方面的经验。
I think the second URL points to dynamic page and you should first call it using GET to get the HTML and then to download it. No experience in this field though.