49

I am trying to make a request to a web page using WebRequest class in .net. The url that I am trying to read requires Windows Authentication due to which I get an unauthorised exception. How can I pass a windows credentials to this request so that it can authenticate.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( "http://myapp/home.aspx" );

request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential( "username", "password", "domain" );

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Raises Unauthorized Exception

this.Response.Write( response.StatusCode );

The above code returns the following error.

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

I noticed one thing while checking the exception details is that the url that I am trying to access is redirecting to a different url which is prompting me to provide the NT login details. I believe that the credentials should get forwarded to this url as well. But apparently it is not happening.

Hemanshu Bhojak
  • 16,972
  • 16
  • 49
  • 64

4 Answers4

52

You should use Credentials property to pass the windows credentials to the web service.

If you wish to pass current windows user's credentials to the service then

request.Credentials = CredentialCache.DefaultCredentials;

should do the trick. Otherwise use NetworkCredential as follows:

request.Credentials = new NetworkCredential(user, pwd, domain);
N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • As you can see in the code which I have posted, I am using the NetworkCredential class to pass my windows login. But even that is not working. – Hemanshu Bhojak Aug 25 '10 at 05:35
  • Did you put the code later - I don't recall seeing it. Regardless, 401 error means credentials can be wrong or it can also mean that credentials are right but that user does not have access to that URL. I will suggest that you check your service url from browser. If it works from there then it should work with WebRequest as well as. – VinayC Aug 25 '10 at 06:00
  • I edited the post and included the code. Also, I am able to access the url in the browser with the credentials but not able to do the same using WebRequest. – Hemanshu Bhojak Aug 26 '10 at 07:23
  • 1
    Try with request.PreAuthenticate = false; Reasoning is that server is throwing 401 as a challenge response and WebRequest would not respond to the challenge unless this property is false. – VinayC Aug 26 '10 at 09:11
  • Tough luck! Just last suggestion - use tool such as Fiddler (www.fiddler2.com) and compare request-response chain from browser (to service) with the same from WebRequest. As you have said that service works from browser, by comparing request-response, you should get the idea where the problem is. – VinayC Aug 27 '10 at 04:39
  • Check your headers on the `exception.Response` - for me I had issues with my proxy interfering. – Troy Parsons Apr 03 '12 at 03:11
  • `request.PreAuthenticate` should be true. If you're doing an inner `WebRequest` instead of this `HttpWebRequest`, you have to set `.Credentials` on that object, too. – vapcguy Apr 21 '20 at 06:09
14

For authenticating to WebService, use DefaultNetworkCredentials instead of DefaultCredentials:

request.Credentials = CredentialCache.DefaultNetworkCredentials;
Vitaliy Ulantikov
  • 10,157
  • 3
  • 61
  • 54
  • 2
    This fixed a problem I've been trying to fix for a week. Thank you! – empz Jan 10 '13 at 21:01
  • I'm wondering if it could help to fix my current issue : https://stackoverflow.com/questions/67335532/windows-authentication-website-chrome-iis-and-sharepoint-authentication ? I read contradictory things regarding differences between each of them. I'll try ASAP. – AFract May 15 '21 at 04:46
10

I am trying to access a link A passing the windows credentials. Link A then redirects me to link B automatically but does not pass the windows credentials which I had supplied. Hence the error. I did request.AutoRedirect = false, and looped through every time I get location in the header i.e. I do my redirects manually each time passing the windows credentials.

This worked for me :)

Hemanshu Bhojak
  • 16,972
  • 16
  • 49
  • 64
7

Using VS2015, request.UseDefaultCredentials = true; works for me!

Rob Bowman
  • 7,632
  • 22
  • 93
  • 200