Thursday, February 14, 2008

ASP .NET Web Service: “The request failed with HTTP status 401: Access Denied.” Error

You may face this error when trying to access a Web Service from your ASP.NET Application. This is because the Anonymous access authentication for the Web Service is turned off. I also was facing this error before. However, I found out 2 ways to resolve this error, there are:

1. Enable Anonymous Access in the IIS Directory Security
2. Assign the Credential Cache Programmatically

1. Enable Anonymous Access in the IIS Directory Security
To do this, try the following steps:

  1. Click [Start] -> [Run] -> Type “inetmgr” and press [OK] or [Enter] key to open IIS Control panel.
  2. Expand the appropriate nodes and navigate to the virtual directory of your Web Service Application.
  3. Select the Virtual Directory, right click and select [Properties].
  4. Click to [Directory Security] Tab and then click [Edit] button.
  5. Check the [Anonymous access] check box.
  6. Click [OK] twice to exit.

2. Assign the Credential Cache Programmatically
You also can programmatically to allow the permissions by specifying the credential cache.

Let say WS is the object of your Web Service (WebService1 which has the default Method HelloWorld.)

C#
WebService1 WS = new WebService1();
WS.Credentials = System.Net.CredentialCache.DefaultCredentials;
Response.Write(WS.HelloWorld());


Visual Basic
Dim WS As New WebService1
WS.Credentials = System.Net.CredentialCache.DefaultCredentials
Response.Write(WS.HelloWorld())

0 comments: