Friday, December 11, 2009

Webservice: Access Denied

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.


//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;

Suppose if you want supply your windows credential use the bellow code:
//Assigning DefaultCredentials to the Credentials property
//of the Web service client proxy (myProxy).
myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;

Reference: http://support.microsoft.com/kb/811318