Search code examples
c#web-serviceswcfsoapusernametoken

C# WCF Service Reference - can not create UserNameToken


In my desktop application (C#, WPF, WCF, .NET4.0) I addes a service reference.

This is Web Service (SOAP) using certificate and UserNameToken in Soap Envelope Header.

I add service reference (Solution -> Service Reference -> Add Service Reference) using WSDL of this web service. In my app.config I have got:

    <customBinding>
<binding name="tmsIntegrationServiceSOAP">
          <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'urn:CDM/tmsIntegrationService/':    -->
          <!--    <wsdl:binding name='tmsIntegrationServiceSOAP'>    -->
          <!--        <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:SupportingTokens>    -->
          <mtomMessageEncoding messageVersion="Soap11" />
          <httpsTransport />
        </binding>
      </customBinding>

<endpoint address="https://XXX/CDM/tmsIntegrationService"
        binding="customBinding" bindingConfiguration="tmsIntegrationServiceSOAP"
        contract="RABEN.GS1.tmsIntegrationService" name="tmsIntegrationServiceSOAP" />

I'm passing user name and password like this:

var ssc = new GS1.tmsIntegrationServiceClient();
ssc.ClientCredentials.UserName.UserName = "test";
ssc.ClientCredentials.UserName.Password = "testPassword";
ssc.Endpoint.Behaviors.Add(new InspectorBehavior());

When I get the request XML (with message inspector) I see that there is no UserNameToken in soap header

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
    <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPoy3hcoBMqQ5Kme7yqEiHKs0AAAAAyxy+QnWD8U60kqJZWaGfvYD8RN14nUVIjC0RuEyVBa8ACQAA</VsDebuggerCausalityData>
  </s:Header>
  <s:Body ...

I try to user basicHttpBinding:

<binding name="secured">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>

<endpoint address="https://XXX/CDM/tmsIntegrationService"
        binding="basicHttpBinding" bindingConfiguration="secured" contract="RABEN.GS1.tmsIntegrationService"
        name="tmsIntegrationServiceSOAP" />

End the result:

<s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
  </s:Header>

When i try to use wsHttpBinding:

<wsHttpBinding>
        <binding name="RabenBinding">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>

<endpoint address="https://XXX/CDM/tmsIntegrationService"
        binding="wsHttpBinding" bindingConfiguration="RabenBinding"
        contract="RABEN.GS1.tmsIntegrationService" name="tmsIntegrationServiceSOAP" />

The result:

<s:Header>
    <a:Action s:mustUnderstand="1">urn:CDM/tmsIntegrationService/importTransportInstruction</a:Action>
    <a:MessageID>urn:uuid:701a0fff-c4aa-4f37-a299-ec6d272e51e7</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
  </s:Header>

Could some one tell me what I'm doing wrong?

EDIT - 2017-06-21 20:00 The certificate is installed on my local machine

<behaviors>
      <endpointBehaviors>
        <behavior name="RabenBehavior">
          <clientCredentials>
            <clientCertificate findValue="this is footprint of certificate"
              storeLocation="CurrentUser" storeName="My" x509FindType="FindByThumbprint" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>

I'm attaching this behavion in endpoint:

<endpoint address="https://XXX/CDM/tmsIntegrationService"
        behaviorConfiguration="RabenBehavior" binding="wsHttpBinding"
        bindingConfiguration="RabenBinding" contract="RABEN.GS1.tmsIntegrationService"
        name="tmsIntegrationServiceSOAP" />

EDIT 2017-06-21 20:58 I try to use <mtomMessageEncoding messageVersion="Soap11" />

<binding name="myCustomBindingConfig">
          <security defaultAlgorithmSuite="Default"
                    authenticationMode="UserNameOverTransport"
            requireDerivedKeys="true"
          includeTimestamp="false" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" />
          <mtomMessageEncoding messageVersion="Soap11" />
          <httpsTransport maxReceivedMessageSize="2000000000" />
        </binding>

But the header is still do not containt user name token

<s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
    <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo0OlG2DVOLdKniJmYU2kvwUAAAAAujMM+x53aEWJYYd4GKyk+PlCKXIih9xLrE0V5TayKhQACQAA</VsDebuggerCausalityData>
  </s:Header>

EDIT 2017-06-21 21:36 Try to use <textMessageEncoding messageVersion="Soap11" /> in custom binding generatet by VS

<binding name="tmsIntegrationServiceSOAP">
          <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'urn:CDM/tmsIntegrationService/':    -->
          <!--    <wsdl:binding name='tmsIntegrationServiceSOAP'>    -->
          <!--        <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:SupportingTokens>    -->
          <textMessageEncoding messageVersion="Soap11" />
          <httpsTransport />
        </binding>

But it bring no effect - there is no user name token in request XML:

<s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:CDM/tmsIntegrationService/importTransportInstruction</Action>
    <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo0wJNixU4QRBhGEUAC3pw50AAAAA/fPJ+e50KUSGbYXwcmoGwgqymCvLXJZIhKg/nKdV97cACQAA</VsDebuggerCausalityData>
  </s:Header>

Solution

  • but what happend if you add by click on the solution explorer add service reference, what it is telling you?. Or you don't have this option.