title: Invoking java webservice from c#.net using client certificate authentication
body: <p>We have a SOAP 1.1 based java web service which requires client certificate for authentication. Internally it uses Apache WSS4J library for certificate validation. We also have a working Java client from which requests with client certificate gets successfully posted to the web service. The web service could not be upgraded to SOAP 1.2 as it is a legacy system.</p>
  
  <p>However, the C#.net code for connecting to the same java webservice doesn't work. We have tried basic as well as custom bindings.</p>
  
  <p>Here is the code for the java client that works:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  
      //create a web service client
      ImportWsImplService srv = new ImportWsImplService(
                                      new URL("https://example.com/ws/import?wsdl"));
  
      ImportWs wsClient = srv.getWsImplPort();
  
      Client client = ClientProxy.getClient(wsClient);
      Endpoint cxfEndpoint = client.getEndpoint();
  
      //change the endpoint url in the wsdl is with http we need to call on https. 
      String endpointURL = "https://example.com/ws/import";
      BindingProvider bp = (BindingProvider)wsClient;
      bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
  
  
      Map&lt;String, Object&gt; outProps = new HashMap&lt;String, Object&gt;();
  
      outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
      outProps.put(WSHandlerConstants.SIGNATURE_USER, "&lt;&lt;key store alias&gt;&gt;");
      outProps.put(WSHandlerConstants.SIG_KEY_ID, "DirectReference");
      outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, App.class.getName());
  
  
      /* client_sign.properties contains
                      org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
                      org.apache.ws.security.crypto.merlin.keystore.type=jks
                      org.apache.ws.security.crypto.merlin.keystore.password=kpassowrd
                      org.apache.ws.security.crypto.merlin.keystore.alias=myalias
                      org.apache.ws.security.crypto.merlin.keystore.file=keystorefile.jks
      */
      outProps.put(WSHandlerConstants.SIG_PROP_FILE, "client_sign.properties");
  
      WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
      cxfEndpoint.getOutInterceptors().add(wssOut);
  
      Response result = wsClient.import();
  
      System.out.println("result=" + result);
  }
  
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  
      WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
  
      pc.setPassword("&lt;&lt;password for key store&gt;&gt;");
  }
  </code></pre>
  
  <h2>Here is my C#.net code and binding from the app.config file:</h2>
  
  <pre><code>X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  store.Open(OpenFlags.ReadOnly);
  X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "&lt;&lt;myclientcertthumbprint&gt;&gt;", true);
  
  var request = new importRequest();
  ImportWSClient client = new ImportWSClient();
  
  client.ClientCredentials.ClientCertificate.Certificate= certs[0];
  
  var response = client.import(request);
  </code></pre>
  
  <h2>Here is my custom binding in app.config</h2>
  
  <pre><code>&lt;customBinding&gt;
      &lt;binding name="AcknowledgeExportCustomersWSImplServiceSoapBinding"&gt;
    &lt;textMessageEncoding messageVersion="Soap11WSAddressing10"   /&gt;
    &lt;security messageProtectionOrder="SignBeforeEncryptAndEncryptSignature"
              messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
             authenticationMode="MutualCertificateDuplex" &gt;
    &lt;/security&gt;
    &lt;httpsTransport  maxReceivedMessageSize="2000000000" &gt;&lt;/httpsTransport&gt;
  &lt;/binding&gt;
  &lt;/customBinding&gt;
  </code></pre>
  
  <p>Can someone help me with this?</p>