PRB: "Access Denied" Error Message When You Call a Web Service While Anonymous Authentication Is Turned Off (811318)
The information in this article applies to:
- Microsoft Web Services (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Web Services (included with the .NET Framework) 1.0
SYMPTOMSWhen 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. CAUSEWhen Anonymous access authentication is turned off for the
Web service application, all the caller applications must provide the
credentials before making any request. By default, the Web service client proxy
does not inherit the credentials of the security context where the Web service
client application is running.RESOLUTIONTo 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;
Visual Basic .NET Sample'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.REFERENCES For
additional information, click the following article number to view the article
in the Microsoft Knowledge Base: 301273
HOW TO: Write a Simple Web Service by Using Visual Basic .NET
308359 HOW TO: Write a Simple Web Service by Using Visual C# .NET
For more information, visit the following
Microsoft Web sites:
Modification Type: | Minor | Last Reviewed: | 7/8/2005 |
---|
Keywords: | kbAuthentication kbSecurity kberrmsg kbWebForms kbprb KB811318 kbAudDeveloper |
---|
|