我目前正在开发一个wcf服务器,并希望从文件/资源而不是证书存储中加载我的证书,以便更轻松地部署。任何想法如何做到这一点?
感谢您的帮助!
答案 0 :(得分:9)
假设您使用的是双工通道,则可以从文件中加载证书,如下所示:
//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's uri"));
host.Credentials.ServiceCertificate.Certificate = certificate;
//configure your server to accept client's certificate , accept all
//certificate in this case, or you can assign it to the public key file
host.Credentials.ClientCertificate.Authentication.CertificateValidationMode
= X509CertificateValidationMode.None;
在客户的代码中,加载证书与上面相同
//configure your client to use certificate
var channelFactory = new ChannelFactory<IYourService>();
channelFactory.Credentials.ClientCertificate.Certificate =
clientCertificate;
//configure your client to accept server's certificate,
//again, for simplicity, just accept any server's certificate
channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode
= X509CertificateValidationMode.None;
我认为从这一点开始你应该没问题。请记住,如果从文件加载,则必须加载由pvk2pfx.exe生成的.pfx文件,它具有私钥和公钥。否则,WCF会对查找私钥的位置感到困惑。
答案 1 :(得分:0)
我认为这就是你要找的东西: http://www.codeproject.com/KB/WCF/wcfcertificates.aspx
答案 2 :(得分:0)
以下SO问题有详细的代码示例,说明如何在证书受密码保护的情况下使用。