I'm trying to setup a WCF client to talk with a service. The connection needs to be https (Transport Security) and we need to do message encryption with a public key. We do not have a client certificate to present.
var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
// This is failing because the private key for client authentication is not being set
// binding.Security.Mode = SecurityMode.TransportWithMessageCrediential;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
And then to set the public key I'm doing
client.ClientCredentials.ServiceCertificate.DefaultCertificate = certificate;
Where certificate is an X509Certificate2 object with just the public key.
When I do this it does not encrypt the message. Using Fiddler I see the raw text of the message come across the wire and the service gives an error because it is expecting an encrypted message.
Edit: Added comment about TransportWithMessageCrediential
Transport
orMessage
. You can't have both. You also seem to confuse encryption with credentials. Credentials don't encrypt the message. They tell the server who the caller is. As for Fiddler being able to see your messages, did you let it install a trusted certificate? Because if you do that, then it can do a man-in-the-middle 'attack'. To protect against that, you can do 'certificate pinning', where you check that the server's certificate is what you expect. – Hans Kilian