问题
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