HttpWebRequest with basic authentication fails with 401 error for DefaultNetworkCredentials

谁说胖子不能爱 提交于 2019-12-11 13:22:12

问题


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;

(I also tried DefaultCredentials)

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?

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.


回答1:


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.



来源:https://stackoverflow.com/questions/14402869/httpwebrequest-with-basic-authentication-fails-with-401-error-for-defaultnetwork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!