Search code examples
wcfauthenticationwcf-security

Trouble connecting to API


I am new to WCF/APIs and know little to nothing about security. Let me know if I need to provide any more information.

I am trying to connect to a service using

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ISalesOrderService">
               <security mode="Transport" >
                  <transport clientCredentialType="Basic"></transport>
               </security>
            </binding>
            <binding name="BasicHttpBinding_IDocumentationService">
               <security mode="TransportCredentialOnly" >
                  <transport clientCredentialType="Basic"></transport>
               </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="address1"
            name="BasicHttpBinding_ISalesOrderService" 
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_ISalesOrderService"
            contract="SoCalls.ISalesOrderService" />
        <endpoint address="address2"
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_IDocumentationService"
            contract="DocCalls.IDocumentationService" 
            name="BasicHttpBinding_IDocumentationService" />
    </client>
</system.serviceModel>

With this, I get this error:

'System.ServiceModel.Security.MessageSecurityException'

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

Edit I followed the instructions suggested in the link provided in the comments, still is giving me this error. I updated my code but I think I am still a bit confused on whether to use HTTP/HTTPS due to lack of knowledge of either service.

Here is how I instantiate my service:

private static SoCalls.SalesOrderServiceClient CreateSalesOrderServiceClient()
    {
        BasicHttpBinding myBinding = new BasicHttpBinding();
        myBinding.MaxReceivedMessageSize = 10000 * 2;
        myBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        EndpointAddress ea = new EndpointAddress("address1");
        SoCalls.SalesOrderServiceClient client = new SoCalls.SalesOrderServiceClient();
        client.ClientCredentials.UserName.UserName = ("username");
        client.ClientCredentials.UserName.Password = ("password");            
        return client;
    }

Solution

  • As it seems, I did not have access to the API with this specific database which is what was giving me my error. I tried sending other credentials for a separate database from the same server and it worked perfectly fine. Purchased the license required and the code works as expected.