自托管 WCF 服务器 - 从文件而不是证书存储加载证书

发布于 2024-10-07 22:08:48 字数 84 浏览 5 评论 0原文

我目前正在使用 wcf 服务器,希望从文件/资源​​而不是证书存储加载我的证书,以使部署更容易。有什么想法如何做到这一点?

感谢您的帮助!

I'm currently working on a wcf server and would like to load my certificate from a file/resource instead of the certificate store to make deployment easier. Any ideas how to do this?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2024-10-14 22:08:48

假设您使用的是双工通道,您可以按如下方式从文件加载证书:

//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 将会对在哪里查找私钥感到困惑。

Suppose you are using duplex channel,you can load certificate from file as the following:

//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;

In your client's code, load the certificate as same as above

//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;

I think you should be okay from this point. Just remember that if you load from a file, you have to load the .pfx file which is generated by pvk2pfx.exe , it has both private key and public key. Otherwise WCF will get confused to where to lookup for private key.

蓝眼泪 2024-10-14 22:08:48

我想这就是您正在寻找的:
http://www.codeproject.com/KB/WCF/wcfcertificates.aspx

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

折戟 2024-10-14 22:08:48

以下SO问题有一个详细的代码示例,说明如何执行此操作,但是在证书受密码保护的情况下,这可能不起作用。

带有证书的编程式 WCF 消息安全

The following SO question has a detailed code sample of how to do this, however this may not work where the certificates are password-protected.

Programmatic WCF Message Security with Certificates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文