.netwcfsoapwcf-bindingwcf-security

C#/WCF not able to make same request as in SOAP ui using wsdl


I have a WSDL where I can send payload successfully through soap, There is necessity to select pre-emptive auth and auth is basic as shown in image.

sample soap ui

#Auth header that is expected-

    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken wsu:Id="test" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username> username</wsse:Username> 
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"> password </wsse:Password>
    </wsse:UsernameToken> 
</wsse:Security>

Have tried many bindings while creating client~ as per doc it should have been Username

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; ```
--------------
var bindingws = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
 var bindinghttps = new BasicHttpsBinding(BasicHttpsSecurityMode.TransportWithMessageCredential);
bindinghttps.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;```

----------------
var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
basicHttpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;```

InboundPortClient client = new InboundPortClient(basicHttpBinding, endpointAddress);
            
client.ClientCredentials.UserName.UserName = "";
client.ClientCredentials.UserName.Password = "";

client.Open();//ok

client.updateCall(null);

//error-System.ServiceModel.ProtocolException: 'The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were:--access denied page

Basically what I understood is binding is not correct looking at the response any ideas how to mimic soap in client using wcf/.net framework .


Solution

  • Setup headers manually for every request

    
    var emptybinding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
     //create client
     ServiceReference.InboundPortClient client = new
       ServiceReference.InboundPortClient(emptybinding,
       endpointAddress);
    
     // expects username 
    client.ClientCredentials.UserName.UserName = "xyz";
              client.ClientCredentials.UserName.Password = "123";
    using (new OperationContextScope(client.InnerChannel))
                {
     // Add a HTTP Header to an outgoing request
     string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["Authorization"] = auth;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
    client.Doyourstuffhere();
                }