0
votes

I'm trying to connect to off-site asmx-service. I have asmx url and login-password for authentication. I've add Service Reference to the service and VS 2010 generated WCF-client with config:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serviceSoap" />
        </basicHttpBinding>
        <customBinding>
            <binding name="serviceSoap12">
                <textMessageEncoding messageVersion="Soap12" />
                <httpTransport />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://tourml.danko.ru:9191/Service.asmx"
            binding="basicHttpBinding" bindingConfiguration="serviceSoap"
            contract="DankoTourMLService.serviceSoap" name="serviceSoap" />
        <endpoint address="http://tourml.danko.ru:9191/Service.asmx"
            binding="customBinding" bindingConfiguration="serviceSoap12"
            contract="DankoTourMLService.serviceSoap" name="serviceSoap12" />
    </client>

I've created client with basicHttpBinding and set credentials. Then I'm trying to call service method:

var service = new serviceSoapClient("serviceSoap");

service.ClientCredentials.UserName.UserName = username;
service.ClientCredentials.UserName.Password = password;            

var items = service.GetItemList();

But here System.ServiceModel.Security.MessageSecurityException is thrown:

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

with inner System.Net.WebException:

{"The remote server returned an error: (401) Unauthorized."}

What's wrong with ClientCredentials setting? Should I use another binding, not basicHttpBinding?

2
Try creating your service like this => var service = new serviceSoapClient(); you probably specified the wrong binding. - woutervs
I've tried both of them, but the same error occurs - WabiSabi

2 Answers

0
votes

http://msdn.microsoft.com/en-us/library/ms732391(v=vs.110).aspx According to the above. When you need to specify credentials, the endpoint should have the security set. This is not the case in your configuration.

Two possibilities => the addservice reference could not create the correct configuration (unlikely). second => you must specify the credentials on the service rather than on the transport.

var service = new serviceSoapClient("serviceSoap");

//service.ClientCredentials.UserName.UserName = username;
//service.ClientCredentials.UserName.Password = password;            

service.setCredentials() (or something)
var itmes = service.getItems();

You should lookup the documentation of the service.

-1
votes

I know this is an old post, but recently I had the same problem and maybe will help others how did I solved it. I'm having a .net web app, consuming a SOAP service for a payment system, with basic authentication. The solution was to use the client credential with a 'u' in front of the username, and also to specify a binding transport in the web.config.

client.ClientCredentials.UserName.UserName = "uMyUsername";
client.ClientCredentials.UserName.Password = "MyPassword";

And in web.config:

<bindings>
  <basicHttpBinding>
    <binding name="XYZ">
      <security mode="Transport" >
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
    <binding name="XYZ" />
  </basicHttpBinding>
</bindings>