以编程方式将证书导入IIS?

Dav*_*ave 14 c# iis ssl iis-7 certificate

我有一个SSL的.pem证书,我想用我的Web应用程序在MSI中分发它(必须在客户的计算机上运行).然后我需要导入它(进入一些凭证存储?)并告诉我的网站绑定使用它.但是我怎么能在代码中做到这一点?我发现了Microsoft.Web.Administration,但不确定从哪里开始......

这是在IIS7顺便说一句.

编辑:这里的目标是拥有一个客户可以在其内部网上运行的Web应用程序.它主要作为iPhone应用程序的API.(也许这不是最好的设计,但我们现在已被锁定.)所以客户安装MSI,瞧,他们有一个Web服务.现在需要在iPhone和Web服务之间进行密码验证; 最简单的方法似乎是在https中完成.所以我做了一个自签名的证书.

我知道重新分发单一证书通常是一个坏主意,但我们只是试图在这里击败偶然的黑客...这只是内联网而且仅限企业,似乎任何人都不可能做任何事情太疯狂了,API严重限制了你能够对数据库做的坏事的数量.

所以,我们的目标是在Intranet Web应用程序上进行密码身份验证,只需单击(ish)安装即可.:-D

Dav*_*ave 25

亲爱的读者,答案是这样的:

// Assume 'site' is already set to your site via something like 
// Site site = mgr.Sites.Add(siteName, directory, 443);

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

// Here, directory is my install dir, and (directory)\bin\certificate.pfx is where the cert file is.
// 1234 is the password to the certfile (exported from IIS)
X509Certificate2 certificate = new X509Certificate2(directory + @"\bin\certificate.pfx", "1234");

store.Add(certificate);

var binding = site.Bindings.Add("*:443:", certificate.GetCertHash(), store.Name);
binding.Protocol = "https";
store.Close();
Run Code Online (Sandbox Code Playgroud)

感谢这个随机的线程:http://forums.iis.net/t/1163325.aspx

  • 不要忘记调用mgr.CommitChanges(). (5认同)