我有 SSL 的 .pem 证书,我想将它与我的 Web 应用程序一起分发到 MSI 中(必须在客户端的计算机上运行)。然后我需要将其导入(导入某些凭据存储区?)并告诉我的站点绑定(bind)使用它。但是我怎样才能在代码中做到这一点呢?我发现了 Microsoft.Web.Administration,但不确定从那里去哪里......
顺便说一句,这是在 IIS7 中。
编辑:这里的目标是拥有一个客户可以在其内部网上运行的 Web 应用程序。它主要充当 iPhone 应用程序的 API。 (也许这不是最好的设计,但我们现在已经确定了。)所以客户安装了 MSI,瞧,他们有一个 Web 服务。现在需要在iPhone和web服务之间进行密码验证;最简单的方法似乎是在 https 中进行。所以我制作了一个自签名证书。
我知道重新分发单个证书通常不是一个好主意,但我们只是想在这里打败临时黑客……这将仅适用于企业内部网,似乎没有人会这样做做任何事情都太疯狂了,而且 API 严格限制了你能够对数据库做的坏事的数量。
我们开始吧,目标是通过一键式(大概)安装在 Intranet Web 应用程序上进行密码验证。 :-D
最佳答案
亲爱的读者,答案是这样的:
// 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();
感谢这个随机线程:http://forums.iis.net/t/1163325.aspx
关于c# - 以编程方式将证书导入 IIS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498291/