具有基本身份验证的HttpWebRequest失败,DefaultNetworkCredentials出现401错误 [英] HttpWebRequest with basic authentication fails with 401 error for DefaultNetworkCredentials
问题描述
我正在使用本地Web服务器(IIS,Windows 8)测试应用程序,该站点需要基本身份验证,并且只要我明确地传递我的凭据,一切都可以正常进行:
I am testing an application with a local Web server (IIS, Windows 8), the site requires Basic authentication, and as long as I explicitly pass my credentials, everything works fine:
request.Credentials = new NetworkCredential("User", "Password", "Domain");
request.PreAuthenticate = true;
但是我想使用集成安全性,因此我尝试如下更改第一行:
But I would like to use integrated security, so I tried to change the first line as follows:
request.Credentials = CredentialCache.DefaultNetworkCredentials;
(我也尝试了DefaultCredentials)
(I also tried DefaultCredentials)
我检查了Fiddler的网络流量,发现在第二种情况下Http请求是在没有授权标头的情况下发送的,因此失败就不足为奇了.但是为什么呢?
I checked the network traffic with Fiddler, and I see that in the second case Http request is sent without an authorization header, so it's not surprising that it fails. But why?
更新.我相信我误解了默认凭据概念. DefaultNetworkCredentials可能不用于生成基本身份验证标头,它必须是用户/密码对.因此,这种行为是设计使然.
UPDATE. I believe I misunderstood default credentials concept. DefaultNetworkCredentials may not be used to generate basic authentication header, it must be a user/password pair. So this behavior is by design.
推荐答案
感谢对此主题发表评论的人,我意识到我对默认凭据的概念有误解.如果仅启用了基本身份验证,则不能使用它们,因此这是正确的行为.
Thanks to people who commented on the topic, I realized that I misunderstood the concept of default credentials. They can't be used in the case then only Basic authentication is enabled, so this is the correct behavior.
这篇关于具有基本身份验证的HttpWebRequest失败,DefaultNetworkCredentials出现401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!