1
votes

I keep getting the following error when trying to consume a webservice:

The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm'.

The webservice is REST written with WCF. The authentication is basic over https.

Any help fixing the error would be apreciated.

Here is the code I tried:

    WebHttpBinding webBinding = new WebHttpBinding();
    webBinding.Security.Mode = WebHttpSecurityMode.Transport;
    webBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    ChannelFactory<ServiceReferences.BTService.FLDT_WholesaleService> factory = new ChannelFactory<ServiceReferences.BTService.FLDT_WholesaleService>(webBinding,
                                                                        new EndpointAddress(
                                                                            "https://wholesale.fluidata.co.uk/FLDT_BT_wholesale/Service.svc"));
    factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
    factory.Credentials.UserName.UserName = "username";
    factory.Credentials.UserName.Password = "password";

    ServiceReferences.BTService.FLDT_WholesaleService proxy = factory.CreateChannel();

    proxy.AvailabilityCheck("123");
2
Are you hosting the REST service on IIS?Codo
@Codo Yes. I am hosting it in IISTom Squires
Who is supposed to check username and password? IIS or the WFC service?Codo
IIS. I did check the username and password are correct by going to the endpoint in my browser.Tom Squires

2 Answers

1
votes

As long as you expose RESTful service you may attempt to use Fiddler - http://www.fiddler2.com/fiddler2/ and/or normal HttpRequest/HttpResponse. Did you try anything like that?

0
votes

Mr. Franek's answer is useful - you WILL be using Fiddler in working with WCF, period. I can add a little bit...what is happening here is that you've specified "Basic" as your authentication scheme as a client. The server is saying "I only allow 'Basic Realm'" as the authentication scheme. What is 'realm'? Basically a credential namespace:

Realm for HTTP basic authentication

Here's another helpful link: Authentication in WinHTTP

I can't find a property or method overload that carries Realm...I would probably try to construct the Authenticate-WWW header manually.

That would go something like this:

request.Headers.Add("WWW-Authenticate", string.Format("basic realm=\"{0}\", realm));

"realm" would be the value of whatever the server is expecting, e.g., "www.targetsite.com".