您现在的位置: 365建站网 > 365文章 > iis中通过ServerManager动态为站点绑定ssl证书代码

iis中通过ServerManager动态为站点绑定ssl证书代码

文章来源:365jz.com     点击数:611    更新时间:2019-06-21 00:30   参与评论

通过ServerManager类对iis进行相对应的操作(包括建立站点,程序池,绑定域名之类,这里不详细解释,网上有很多这方面的资料),我就想既然可以手动在iis创建域名并绑定ssl证书,那应该也有对应的实现方法,果然找到了里面的一个方法,贴上代码先

ServerManager sm = new ServerManager();

string bindingInformation = "*:" + port + ":" + bindingDomainName;

sm.Sites[webName].Bindings.Add(bindingInformation, certificateHash, certificateStoreName);

bindingInformation应该不用解释,一眼就看到是绑定的端口和域名, certificateHash和certificateStoreName又是什么呢?certificateHash参数类型是byte[],而certificateStoreName参数类型是string,通过直接百度翻译得到的结果是证书散列和证书,正在思考他们的来源时,突然想到购买ssl证书之后提供给我们的相关文件和秘钥,应该和这里大有关联,通过查询一些资料,得知certificateHash(证书散列)是通过.pfx文件获取而来的,而certificateStoreName则是证书名称,这里贴上完整的代码

</>code

  1.  // <summary>
  2.         /// 增加绑定域名(ssl)
  3.         /// </summary>
  4.         /// <param name="webName">站点名称</param>
  5.         /// <param name="port">端口</param>
  6.         /// <param name="bindingDomainName">绑定域名</param>
  7.         public void AddHostHeaderSSL(string webName, string port, string bindingDomainName)
  8.         {
  9.             //**pfxPath**是指提供给你的.pfx文件的路径,购买ssl之后他会提供给你相关的文件,里面有多个文件
  10.             //一般包含apache文件夹,iis文件夹,nginx文件夹,我使用的是iis部署,所以.pfx文件在iis文件夹里面
  11.             //**certPwd**是指购买ssl之后提供给你的秘钥
  12.             string pfx = Directory.GetFiles(pfxPath, "*.pfx", SearchOption.AllDirectories).FirstOrDefault();
  13.             var store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine);
  14.             store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
  15.             var certificate = new X509Certificate2(pfx, certPwd, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
  16.             store.Add(certificate);
  17.             store.Close();
  18.             var certificateStoreName = store.Name;              //绑定的证书名称
  19.             var certificateHash = certificate.GetCertHash();    //证书内容
  20.             ServerManager sm = new ServerManager();
  21.             string bindingInformation = "*:" + port + ":" + bindingDomainName;
  22.             sm.Sites[webName].Bindings.Add(bindingInformation, certificateHash, certificateStoreName);
  23.             sm.CommitChanges();
  24.         }

至此,动态绑定ssl证书的功能就实现了


以下示例演示CertificateHash属性。如果协议标识符为“https”,则显示证书哈希和证书库名称。此代码示例是为Binding提供的更大示例的一部分

</>code

  1. if (binding.Protocol == "https"){
  2.      // There is a CertificateHash and  
  3.      // CertificateStoreName for the https protocol only.
  4.     bindingdisplay = bindingdisplay + "\n   CertificateHash: " + 
  5.         binding.CertificateHash + ": ";    // Display the hash.
  6.     foreach (System.Byte certhashbyte in binding.CertificateHash)
  7.     {
  8.         bindingdisplay = bindingdisplay + certhashbyte.ToString() + " ";
  9.     }
  10.     bindingdisplay = bindingdisplay + "\n   CertificateStoreName: " + 
  11.         binding.CertificateStoreName;}


vb.net

</>code

  1. If (binding.Protocol = "https") Then
  2.     ' There is a CertificateHash and  
  3.     ' CertificateStoreName for the https protocol only.
  4.     bindingdisplay = (bindingdisplay + (""& vbLf&"   CertificateHash: "  _
  5.                 + (binding.CertificateHash + ": ")))
  6.     ' Display the hash.
  7.     For Each certhashbyte As Byte In binding.CertificateHash
  8.         bindingdisplay = (bindingdisplay  _
  9.                     + (certhashbyte.ToString + " "))
  10.     Next
  11.     bindingdisplay = (bindingdisplay + (""& vbLf&"   CertificateStoreName: " + binding.CertificateStoreName))
  12. End If


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (611人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号