如何在代码中将测试证书绑定到iis网站 [英] How to Bind a test certificate to an iis website in code
问题描述
我有一个要安装和设置的Web服务测试证书.
我按如下所示安装证书
I have a test certifcate that I want to install and setup for a webservice I am installing.
I install the certificate as follows
X509Certificate2 cert = new X509Certificate2(Settings.CertFile, Settings.CertPass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
cert.FriendlyName = Settings.CertName;
X509Store store = new X509Store(Settings.StoreName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite); //OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
这样做的原因是该证书在iis管理器中可见,问题是该证书未绑定到我的Web服务所安装的站点.我可以通过编辑将该证书手动绑定到IIS管理器中的站点网站绑定但我希望能够在代码中做到这一点,以便我可以安装和运行.如果您使用selfssl,它将使用siteID参数将证书安装到站点,我想模仿
This works in that the certifcate is visible in iis manager, Problem is that the certificate is not tied to the site that my webservice is installed under. I can manually bind it to the site in IIS manager by editing the site binding but I want to be able to do this in code so that I can just install and run. If you use selfssl it installs the certificate to a site using the siteID parameter, I want to mimic this in my code?
推荐答案
对此有两种解决方案:
如果使用IIS7
它带有.net api Microsoft.Web.Administration.dll
可以直接引用它,也可以在运行时加载它,它使您可以获取网站绑定,并查看是否附加了证书,删除证书或将一个证书添加到绑定中.
如果您需要同时使用iis7和iis6的功能,则需要使用我们的httpapi.dll,这是一个Win32 dll,因此从.net起需要进行一些拼字游戏工作
该链接被证明是无价的
http://www.pinvoke.net/default.aspx/httpapi.httpqueryserviceconfiguration [ ^ ]
There are a couple of solutions to this:
If using IIS7
It comes with a .net api Microsoft.Web.Administration.dll
This can be reference directly or loaded at runtime it allows you to get a sites bindings and see if a certificate is attached, remove a certificate or add one to a binding.
If you need something to work with both iis7 and iis6 then you need to us httpapi.dll which is a win32 dll so from .net requires some pinvoke work
This link proved invaluable
http://www.pinvoke.net/default.aspx/httpapi.httpqueryserviceconfiguration[^]
这篇关于如何在代码中将测试证书绑定到iis网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!