Note: This is KB article is taken from MSDN as it is for my own reference.
Symptoms
When you try to call a Web service application and Anonymous access authentication is turned off, you may receive the following error message.
The request failed with HTTP status 401: Access Denied.
Description: An unhandled exception occurred during the execution of the current Web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with HTTP status 401: Access Denied.
Cause
Resolution
To resolve this problem, you must use the Credentials property of the Web service client proxy to set the security credentials for Web service client authentication.
To set the Credentials property, do one of the following:
First Method
Assign the DefaultCredentials to the Credentials property of the Web Service Proxy class to call the Web service while Anonymous access authentication is turned off. The DefaultCredentials property of the CredentialCache class provides system credentials of the security context where the application is running. To do this, use the following code:
Visual C# .NET Sample
- //Assigning DefaultCredentials to the Credentials property
- //of the Web service client proxy
- (myProxy).myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;
Visual Basic .NET Sample
- //Assigning DefaultCredentials to the Credentials property'of the Web service client proxy
- (myProxy).myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials
Second Method
You may also use the CredentialCache class to provide credentials for Web service client authentication. Create an instance of the CredentialCache class. Create an instance of NetworkCredential that uses the specified user name, password, and domain. Add the NetworkCredential to the CredentialCache class with the authentication type. To do this, use the following code:
Visual C# .NET Sample
- //Create an instance of the CredentialCache class.CredentialCache
- cache = new CredentialCache();
- // Add a NetworkCredential instance to CredentialCache.
- // Negotiate for NTLM or Kerberos authentication.cache.
- Add( new Uri(myProxy.Url), "Negotiate", new NetworkCredential("UserName", "Password", "Domain"));
- //Assign CredentialCache to the Web service Client
- Proxy(myProxy) Credetials property.myProxy.Credentials = cache;
- //Create an instance of the CredentialCache
- class.Dim cache As CredentialCache = New CredentialCache()
- Add a NetworkCredential instance to CredentialCache.
- Negotiate for NTLM or Kerberos
- authentication.cache.Add(New Uri(myProxy.Url), "Negotiate", New NetworkCredential("UserName", "Password", "Domain"))
- Assign CredentialCache to the Web service Client
- Proxy(myProxy) Credetials property.myProxy.Credentials = cache
Note: The CredentialCache class and the NetworkCredential class belong to the System.Net namespace.
For more information about how to set the Credentials property, see the "More Information" section in this article.
Status
This behavior is by design.
More Information
DefaultCredentials represents the system credentials for the current security context where the application is running. For a client-side application, the default credentials are typically the Windows credentials such as user name, password, and domain of the user who is running the program. For ASP.NET programs, the default credentials are the user credentials of the identity for the ASP.NET worker process, or the user who is being impersonated. In the following sample ASP.NET program, DefaultCredentials represents the ASPNET user account (or NetworkService user account for applications run on Microsoft Internet Information Services [IIS] 6.0) because no impersonation is set to the caller.
Steps to Reproduce the Behavior
- Create a new ASP.NET Web Service by using Visual C# .NET or Visual Basic .NET.
- Name the project WebServiceTest.
- By default, Service1.asmx is created.
- Uncomment the default WebMethod "HelloWorld()".
- On Build menu, click Build Solution.
- Turn off Anonymous access to WebServiceTest. To do this, follow these steps:
- In Control Panel, double-click Administrative Tools.
- Double-click Internet Information Services.
- Expand Internet Information Services, and then locate the WebServiceTest virtual directory.
- Right-click WebServiceTest, and then click Properties.
- Click the Directory Security tab.
- Under Anonymous access and authentication control, click Edit.
- In the Authentication Methods dialog box, click to clear the Anonymous access check box.
- Click to select the Integrated Windows authentication check box.
Note: Verify that only Integrated Windows authentication is selected. - Click OK to close the Authentication Methods dialog box.
- Click OK to close Properties.
- On the Build menu, click Build Solution.
- Type the following address in the browser to view the Service1 Web service description:
http://localhost/WebServiceTest/Service1.asmx - Test the HelloWorld WebMethod. The WebMethod works as expected.
- Add a Web Reference to a test ASP.NET Web Application. To do this, follow these steps:
- Create a new ASP.NET Web Application by using Visual C# .NET or Visual Basic .NET. Name the project WebServiceCaller.
- By default, WebForm1.aspx is created.
- In Solution Explorer, right-click References, and then click Add Web Reference.
- In the Address text box, type the URL for WebServiceTest as follows:
http://localhost/WebServiceTest/Service1.asmx - Click Go or press ENTER, and then click Add Reference.
- In Solution Explorer, right-click WebForm1.aspx, and then click View Code.
- Append the following code to the Page_Load event:
Visual C# .NET Sample:
Visual Basic .NET Sample:- //Start an instance of the Web service client-side
- proxy.localhost.Service1 myProxy = new localhost.Service1();
- Response.Write( myProxy.HelloWorld());
- //Start an instance of the Web service client-side
- proxy.Dim myProxy As localhost.Service1 = New localhost.Service1()Response.Write(myProxy.HelloWorld())
- On the Debug menu, click Start, and then view the application in the browser.
- The error message that is discussed in the "Symptoms" section appears in the browser.
- To resolve this problem, assign DefaultCredentials to the Credentials property of the Web service client-side proxy. To do this, insert the following code before the line "Response.Write( myProxy.HelloWorld())":
Visual C# .NET Sample:
Visual Basic .NET Sample:- myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;
- myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials
- Repeat step 13

arun singhPosted Nov 20, 2015, 5:53 AM
We tried below code for calling : C# code : WSDL Url: http://XXX.XXX.XXX.XXX/ws/SmartMeteringServices?wsdl webservice Client=new Client(); NetworkCredential myNetworkCredential = new NetworkCredential("username", "password"); CredentialCache myCredentialCache = new CredentialCache(); myCredentialCache.Add(myUrl, "Digest", myNetworkCredential); Client.PreAuthenticate = true; Client.Credentials = myNetworkCredential; Client.getMeteringSamples(1, 3); 2. But when we tried to invoke any of the method from the service it is showing exception. Below are the error details "401 The HTTP request is unauthorized with client authentication scheme 'Digest'. The authentication header received from the server was 'Digest realm="Sj7Qsdflq", nonce="Zrmhh9tmi4E0ctnTwFte"'. Please help me out in anyway if you can.
Govardhana ReddyPosted Feb 9, 2007, 5:51 AM
Hi, I went through the article. Its really great and would help lot of begineers like me to learn alot and give lot of information I have a similar kind of requirement Can we pass the username and password to a regular html page by using the credentials provided by .Net. Most of the time regular HTMl pages to require the authetication. Whn we want to call such page in code (Written in C#.Net) we won't be able to do so without passing the required parameters. Does anyone know hw to achieve this i.e., to make a call to the regular html pages by passing the required parameters by using the code written in C#.Net. My mail-id is apondu@gmail. please send in the suggestions and code snips at this id Thanks for the help Govardhana