Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

Solving "Access denied" exceptions in WebServices

This morning I wrote the NUnit test class for a WebService method I am currently building. This method stores data in different data stores and uses other webservices to do this. All these services were running on my machine. But when I started NUnit, the test failed when I tried to make a call to one of the other webservices. All I got was: System.Net.WebException: The request failed with HTTP status 401: Access Denied.

Why was I getting this? I checked the access to the directories, to make sure the ASPNET account had enough access rights, and that was OK. Same for the IUSR account. So I tried to call the webmethod by viewing the .asmx file in the webbrowser. That worked, so why was my webservice getting an Access denied result when my webservice called that method? The code seemed to be fine:

public int GetCustomerId(string name)
{
   int
customerId = -1;

   WebserviceCustomer.Interface customer = new WebserviceCustomer.Interface();
   customerId = customer.GetCustomerIdByName(name);
   return customerId;
}

This made me check the internet to see what might be the problem. What I found was that, even when the user credentials are all correct and identical, you need to set the credentials before making the actual call to the other webservice. This posting in particular on http://www.dotnet247.com pointed me to the solution. The code now looks like this:

public int GetCustomerId(string name)
{
   int
customerId = -1;

   WebserviceCustomer.Interface customer = new WebserviceCustomer.Interface();
   // Make sure the credentials for the webservice call are the current credentials
   customer.Credentials = System.Net.CredentialCache.DefaultCredentials;
   customerId = customer.GetCustomerIdByName(name);
   return customerId;
}

Comments

Jan Schreuder said:

A bit off-topic, but why the try catch block when all you do is throw?
# April 14, 2005 4:07 PM

Jan Schreuder said:

I was about to ask the same thing as Ramon...
# April 14, 2005 6:15 PM

Jan Schreuder said:

Point taken. I removed it, as you can see :-) It was there, because the example was based on existing code I found in one of the webservices I was working on. Forgot to remove all for this topic.
# April 14, 2005 8:55 PM

Dheeraj said:

Same problem

# March 26, 2009 10:30 AM

Neethu said:

Same Problem

# July 20, 2010 2:48 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 1 and 7 and type the answer here: