简体   繁体   English

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

[英]Programmatically import cert into IIS?

I have a .pem certificate for SSL, I want to distribute it with my web application in an MSI (has to run on clients' computers). 我有一个SSL的.pem证书,我想用我的Web应用程序在MSI中分发它(必须在客户的计算机上运行)。 I then need to import it (into some credentials store?) and tell my site bindings to use it. 然后我需要导入它(进入一些凭证存储?)并告诉我的网站绑定使用它。 But how can I do this in code? 但是我怎么能在代码中做到这一点? I've discovered Microsoft.Web.Administration, but not sure where to go from there … 我发现了Microsoft.Web.Administration,但不确定从哪里开始......

This is in IIS7 btw. 这是在IIS7顺便说一句。

EDIT: The goal here is to have a web application that customers can run on their intranets. 编辑:这里的目标是拥有一个客户可以在其内部网上运行的Web应用程序。 It mainly acts as an API for an iPhone app. 它主要作为iPhone应用程序的API。 (Maybe this isn't the best design but we're locked in now.) So the customer installs the MSI, and voila, they have a web service. (也许这不是最好的设计,但我们现在已被锁定。)所以客户安装MSI,瞧,他们有一个Web服务。 Now there needs to be password authentication between the iPhone and the web service; 现在需要在iPhone和Web服务之间进行密码验证; the simplest way seemed to be to do it in https. 最简单的方法似乎是在https中完成。 So I made a self-signed cert. 所以我做了一个自签名的证书。

I'm aware that redistributing a single cert is generally a bad idea, but we're just trying to defeat casual hackers here … this is going to be intranet only and for businesses only, it seems unlikely that anyone is going to be doing anything too crazy, and the API severely restricts the amount of Bad Things you are able to do to the database anyways. 我知道重新分发单一证书通常是一个坏主意,但我们只是试图在这里击败偶然的黑客...这只是内联网而且仅限企业,似乎任何人都不可能做任何事情太疯狂了,API严重限制了你能够对数据库做的坏事的数量。

So there we go, the goal is to have password authentication on an intranet web app, with one-click(ish) installation. 所以,我们的目标是在Intranet Web应用程序上进行密码身份验证,只需单击(ish)安装即可。 :-D :-D

The answer, dear readers, is this: 亲爱的读者,答案是这样的:

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

Thanks to this random thread: http://forums.iis.net/t/1163325.aspx 感谢这个随机的线程: http//forums.iis.net/t/1163325.aspx

You need to narrow down your question. 你需要缩小你的问题范围。 What is the certificate used for (exactly)? 用于(确切)的证书是什么?

If your certificate is used for client-side authentication (to authenticate the client on the server), then distributing it with your application makes such authentication plain useless, as you would be disclosing the secret key. 如果您的证书用于客户端身份验证(用于对服务器上的客户端进行身份验证),那么将其与您的应用程序一起分发会使此类身份验证毫无用处,因为您将公开密钥。

If you need to validate the server's certificate (and you've been given server's certificate or certificate chain in your PEM file), then this could work, but why would you need to install the certificate to the certificate store? 如果您需要验证服务器的证书(并且您已在PEM文件中获得服务器的证书或证书链),那么这可能有效,但为什么需要将证书安装到证书存储区?

You should note, that PEM format is not natively supported by Windows or .NET libraries so you need to convert it to PFX before deployment, and then import a PFX or just create a store in memory based on PFX (you will find plenty of information by looking for PFX or PKCS#12 on StackOVerflow). 您应该注意,Windows或.NET库本身不支持PEM格式,因此您需要在部署之前将其转换为PFX,然后导入PFX或仅在内存中创建基于PFX的存储(您将找到大量信息通过在StackOVerflow上查找PFX或PKCS#12)。

Upd: it would be a better approach to generate the certificate each time you install the application and let the user have their own certificate (eg. it's possible that they already have a valid certificate for their web site). 更新:每次安装应用程序并让用户拥有自己的证书(例如,他们的网站可能已经拥有有效证书)时,生成证书将是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM