Selfhosting wcf server - load certificate from file instead of certificate store

I am currently working on a wcf server and want to download my certificate from a file / resource instead of a certificate store to simplify deployment. Any ideas how to do this?

Thank you for your help!

+4
source share
3 answers

Assuming you are using a duplex channel, you can download a certificate from a file as follows:

//Load certificate file with private key var certificate = new X509Certificate2("c:\certificate.pfx", "password"); //Configure your server by to use certificate, for example: var host = new ServiceHost(typeof(YourService), new Uri("Your service uri")); host.Credentials.ServiceCertificate.Certificate = certificate; //configure your server to accept client certificate , accept all //certificate in this case, or you can assign it to the public key file host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; 

In your client code, upload the certificate in the same way as above

 //configure your client to use certificate var channelFactory = new ChannelFactory<IYourService>(); channelFactory.Credentials.ClientCertificate.Certificate = clientCertificate; //configure your client to accept server certificate, //again, for simplicity, just accept any server certificate channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; 

I think everything should be in order from now on. Just remember that if you download from a file, you need to download the .pfx file generated by pvk2pfx.exe, it has both a private key and a public key. Otherwise, WCF is confused with where to look for the private key.

+9
source

I think this is what you are looking for: http://www.codeproject.com/KB/WCF/wcfcertificates.aspx

0
source

The next SO question contains a detailed code example on how to do this, however this may not work where the certificates are password protected.

Software Security WCF Messages with Certificates

0
source

Source: https://habr.com/ru/post/1332119/


All Articles