0

I have a WCF service hosted in IIS6. It is doing simple WebRequest.

When I call it from ASMX service(Hosted in the same IIS6). Everything is working great.
When I call it from WinForms application. Everything is also working as expected.
The problem arises when I call it from another WCF service.
Then response.GetResponseStream() throws 401 unauthorized error.

What am I doing wrong here?

System.Net.WebRequest request = WebRequest.Create(full_path_to_the_webpage);
request.Credentials = new NetworkCredential(username_for_http_login, password_for_http_login);
request.PreAuthenticate = true;
request.UseDefaultCredentials = false;

WebResponse response = request.GetResponse();

Stream responseStream = response.GetResponseStream(); //THIS line returns 401 Unauthorized
Tanel
  • 314
  • 2
  • 10
  • Are you sure that ur wcf service requires custom authentication details while calling APIs from service? Have u done the same thing in asmx web service and winforms? – Uday0119 Jun 24 '12 at 11:03
  • With this webrequest i'm not calling another webservice, but one PHP page with basic authentication. This webrequest is inside my WCF service. The problem arises only when i'm calling this WCF service from another WCF service. When i'm calling this webservice from ASMX or WinForms everything works. My service is using wsHttpBinding. – Tanel Jun 24 '12 at 11:07
  • U said its working fine in winform. So I just want to know, whether you have used authentication there also? Because I don't think wcf service requires authentication as user identity of IIS is used to authenticate in domain environment or user identity in standalone environment. So if ur PHP web server is also authenticated, then I don't think that u require explicit authentication here. U just use UseDefaultCredentials=true – Uday0119 Jun 24 '12 at 11:13
  • Yes. When consuming from winforms I use Windows authentication to access the service. The PHP page i'm calling. Is using different user/pass than my IIS6 configuration. I have no way of changing the PHP page credentials. Other methods inside the service work great if i'm calling them from another WCF. Only this method, containing WebRequest. Fails when calling from another WCF service. – Tanel Jun 24 '12 at 11:17
  • Is your WCF service exposed as a REST service as i see that you are using WebRequest to invoke the WCF service? – Rajesh Jun 25 '12 at 09:28

1 Answers1

0

Turned out that php script needed username to be lowercase only. So this code is happily working now.

Thank you to all who tried to help me.

To Rajesh: No i was not using service itself with WebRequest. I called one php file from another server with WebRequest.

Tanel
  • 314
  • 2
  • 10