1

I am new to Web Services in general, and we are using .Net Framework 4.5.2, anyway I am trying to consume a web service that requires a certificate and a password. I added the certificate gained from the providers in the project properties --> Resources --> file --> add, I also tried to use the SetCertificate() function but It seems to be a little complicated for me so I stick with loading the certificate from the properties as mentioned, however I already set all the binding setting as wanted, but somehow I am missing something, Here is my code:

            string clientUrl = "some wsdl URL goes here";
            BasicHttpsBinding binding = new BasicHttpsBinding

            {
                MaxReceivedMessageSize = Int32.MaxValue,
                MaxBufferSize = Int32.MaxValue,
                SendTimeout = new TimeSpan(0, 15, 0), 
                MessageEncoding = WSMessageEncoding.Text,
                Security = {
                        Mode =  BasicHttpsSecurityMode.Transport,
                    Transport = {
                        ClientCredentialType = HttpClientCredentialType.Certificate
                    }
                }
            };

 ClientsClient testClient = new ClientsClient(binding, new EndpointAddress(new Uri(clientUrl)));
 testClient.ClientCredentials.ClientCertificate.Certificate = LoadCertification();
        private X509Certificate2 LoadCertification()
        {
            byte[] bytes = Properties.Resources.publicCert;
            return new X509Certificate2(bytes, "password");
        }

Note 1: The certificate extenstion is '.p12', It may be a list of certifications, if that is the case!, is it possible to pass them all?. In the code I presented I am always getting The exception:

System.ServiceModel.ProtocolException: The 'Security' header from the namespace 'Some Http url goes here' not was understood by the recipient of the message. The message was not processed. The error usually indicates that the sender of the message has enabled a communication protocol that cannot be processed by the recipient. Verify that the client binding configuration is consistent with the service binding.

I tried to test the web service with "SOAP UI" and it worked, which made me sure that I am doing something wrong with the code, So I appreaciate any possible help that explains how to associate the certifcate in the code in the right way!.

EDIT: in the .p12 file there are 3 certifications, which I tried to add also like this:

            X509Certificate2Collection coll = LoadCertification();
            int count = 0;
            foreach (X509Certificate2 cert in coll)
            {
                testClient.ClientCredentials.ClientCertificate.Certificate = cert;
                count++;// this variable is just to check the number of certificates 
            }

And I modified the loadCertification() method to look like this:

        private X509Certificate2Collection LoadCertification()
        { string certPath = "C:/Users/ISA/Desktop/Progetti/Certificato e password/name.p12";
            

            X509Certificate2Collection coll = new X509Certificate2Collection();
            coll.Import(certPath , "password", X509KeyStorageFlags.DefaultKeySet);
            return coll;

        }
Warios
  • 189
  • 15
  • Where you are using LoadCertification() method? – MBB Jun 26 '20 at 10:42
  • @mahesh_b in the same code block, I just separated them here while writing to make it clearer, if you are referring to the access modifiers. – Warios Jun 26 '20 at 10:45
  • Can I assume ClientsClient is your Service Reference? If so how you are attaching this reference with certificate? Can you please paste that code? – MBB Jun 26 '20 at 12:08
  • @mahesh_b Yeah, the ClientsClient is my proxy class, and I am using `testClient.ClientCredentials.ClientCertificate.Certificate = LoadCertification();` to add to attach the certificate, anyway I edited the code, Hope you can give it a look! – Warios Jun 26 '20 at 12:19
  • I tested your code and did not find any issues.. Looks like your service has some settings . Please check here and try changing basicHttpBinding to WSHttpBinding - https://stackoverflow.com/questions/8038003/wcf-client-passing-username-token-with-mustunderstand-set-to-true – MBB Jun 26 '20 at 12:59
  • Thank you for your time my friend, I tried to change the binding type but it gives an error so it's not the problem, the problem is the cryptography, The file I have contains 3 certificates inside it, and I need to associate all of them – Warios Jun 26 '20 at 13:20

0 Answers0