pdamoc
pdamoc

Reputation: 2923

How can I configure a WSDL client created with Connected Services to use Basic Authentication?

I have a 3-rd party WSDL service that I need to integrate in a .NET Core 2.1 webapp.

I added the service via "Add Connected Service" > "Microsoft WCF Web Service Reference Provider".

I have some utility class that configures the service like this:

ServiceClient servicePort = new ServiceClient(); 

servicePort.ClientCredentials.UserName.UserName = USERNAME;
servicePort.ClientCredentials.UserName.Password = PASSWORD;

setupHttpHeader(servicePort.InnerChannel );

where

    public static void setupHttpHeader( IClientChannel serviceChannel )
    {
        using (new OperationContextScope( serviceChannel ))
        {
            // Add a HTTP Header to an outgoing request
            HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
            requestMessage.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.UTF8.GetBytes(USERNAME + ":" + PASSWORD));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
        }
    }

Now, when I try to call the endpoint in my controller, I get this exception:

System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'basic realm="appbox object-model"'.     

In an old .NET Framework 4.0 Sample for connecting the the 3-rd party service there was an app.config that had some configurations for the bindings. They looked like this:

      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" />
      </security> 

What do I need to add to my project in order to make this work?

Upvotes: 1

Views: 2527

Answers (1)

pdamoc
pdamoc

Reputation: 2923

The solution was found in this issue comment.

In the GetBindingForEndpoint section of the generated code, there needs to be something like:

System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;

In my case I needed TransportCredentialOnly as the the endpoint was http not https.

Upvotes: 3

Related Questions