/// <summary> /// Adds a certificate to the specified Batch account. /// </summary> /// <param name="parameters">The parameters to use when creating the certificate.</param> public void AddCertificate(NewCertificateParameters parameters) { if (parameters == null) { throw new ArgumentNullException("parameters"); } CertificateOperations certOperations = parameters.Context.BatchOMClient.CertificateOperations; Certificate unboundCert; if (!string.IsNullOrWhiteSpace(parameters.FilePath)) { if (string.IsNullOrWhiteSpace(parameters.Password)) { unboundCert = certOperations.CreateCertificate(parameters.FilePath); } else { unboundCert = certOperations.CreateCertificate(parameters.FilePath, parameters.Password); } } else { if (string.IsNullOrWhiteSpace(parameters.Password)) { unboundCert = certOperations.CreateCertificate(parameters.RawData); } else { unboundCert = certOperations.CreateCertificate(parameters.RawData, parameters.Password); } } WriteVerbose(string.Format(Resources.AddingCertificate, unboundCert.Thumbprint)); unboundCert.Commit(parameters.AdditionalBehaviors); }
//设置站点绑定的协议、端口 private bool SetSiteProtolPort(List <string> AListStrSetting, ref string AStrReturn) { bool LBoolReturn = true; string LStrCallReturn = string.Empty; Site LSiteUMPPF = null; int LIntHttpBindingPort = 0; int LintHttpsBindingPort = 0; int LIntNetTcpBindingPort = 0; string LStrCertificateFile = string.Empty; string LStrCertificateHashString = string.Empty; byte[] LByteCertificateHash = null; try { App.GStrCatchException = string.Empty; AStrReturn = "006"; ServerManager LServerManager = new ServerManager(); foreach (Site LSiteSingle in LServerManager.Sites) { if (LSiteSingle.Name.Equals("UMP.PF")) { LSiteUMPPF = LSiteSingle; AStrReturn = string.Empty; break; } } if (AStrReturn == "006") { return(false); } App.WriteLog("SetBinding", string.Format("UMPSite getted.")); LSiteUMPPF.Bindings.Clear(); LServerManager.CommitChanges(); LServerManager.Dispose(); LServerManager = null; LBoolReturn = CertificateOperations.UninstallCertificate(AListStrSetting[0], StoreName.My, StoreLocation.CurrentUser, ref LStrCallReturn); LBoolReturn = CertificateOperations.UninstallCertificate(AListStrSetting[0], StoreName.My, StoreLocation.LocalMachine, ref LStrCallReturn); LBoolReturn = CertificateOperations.UninstallCertificate(AListStrSetting[0], StoreName.Root, StoreLocation.LocalMachine, ref LStrCallReturn); LStrCertificateFile = System.IO.Path.Combine(App.GStrSiteRootFolder, @"Components\Certificates", "UMP.S." + AListStrSetting[0] + ".pfx"); if (File.Exists(LStrCertificateFile)) { File.Delete(LStrCertificateFile); } LBoolReturn = CertificateOperations.CreateCertificate(AListStrSetting[0], ref LStrCallReturn); if (!LBoolReturn) { AStrReturn = "013"; //在当前用户的 My 区域中创建证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } App.WriteLog("SetBinding", string.Format("Server certificate created.\t{0}", LStrCallReturn)); LStrCertificateHashString = LStrCallReturn; IStrBindHashString = LStrCertificateHashString; LBoolReturn = CertificateOperations.ExportCertificate(LStrCertificateHashString, "VoiceCyber,123", LStrCertificateFile, ref LStrCallReturn); if (!LBoolReturn) { AStrReturn = "014"; //从当前用户的 My 区域中导出证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } App.WriteLog("SetBinding", string.Format("Server certificate exported.\t{0}", LStrCertificateFile)); LBoolReturn = CertificateOperations.CertificateIsExist(LStrCertificateHashString, StoreName.My, StoreLocation.LocalMachine, ref LStrCallReturn); if (!LBoolReturn) { if (!string.IsNullOrEmpty(LStrCallReturn)) { AStrReturn = "018"; //检测本计算机的 My 区域中是否安装证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } LBoolReturn = CertificateOperations.InstallCertificate(LStrCertificateFile, "VoiceCyber,123", StoreName.My, StoreLocation.LocalMachine, ref LStrCallReturn); if (!LBoolReturn) { AStrReturn = "015"; //向本计算机的 My 区域中安装证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } App.WriteLog("SetBinding", string.Format("Install server certificate end.\t{0}", "My")); } LByteCertificateHash = CertificateOperations.ObtainCertificateCertHash(LStrCertificateHashString, StoreName.My, StoreLocation.LocalMachine, ref LStrCallReturn); if (LByteCertificateHash == null) { LBoolReturn = false; AStrReturn = "016"; //从本计算机的 My 区域中获取证书的哈希值数组失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } App.WriteLog("SetBinding", string.Format("ObtainCertificateCertHash end.")); LBoolReturn = CertificateOperations.CertificateIsExist(LStrCertificateHashString, StoreName.Root, StoreLocation.LocalMachine, ref LStrCallReturn); if (!LBoolReturn) { if (!string.IsNullOrEmpty(LStrCallReturn)) { AStrReturn = "019"; //检测本计算机的 Root 区域中是否安装证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } LBoolReturn = CertificateOperations.InstallCertificate(LStrCertificateFile, "VoiceCyber,123", StoreName.Root, StoreLocation.LocalMachine, ref LStrCallReturn); if (!LBoolReturn) { AStrReturn = "017"; //向本计算机的 Root 区域中安装证书失败 App.GStrCatchException = LStrCallReturn; return(LBoolReturn); } App.WriteLog("SetBinding", string.Format("Install server certificate end.\t{0}", "Root")); } LServerManager = new ServerManager(); foreach (Site LSiteSingle in LServerManager.Sites) { if (LSiteSingle.Name.Equals("UMP.PF")) { LSiteUMPPF = LSiteSingle; AStrReturn = string.Empty; break; } } LIntHttpBindingPort = int.Parse(AListStrSetting[1]); LintHttpsBindingPort = LIntHttpBindingPort + 1; LSiteUMPPF.Bindings.Add("*:" + AListStrSetting[1] + ":", "http"); LSiteUMPPF.Bindings.Add("*:" + LintHttpsBindingPort.ToString() + ":", LByteCertificateHash, "MY"); LSiteUMPPF.Bindings.Add(AListStrSetting[2] + ":*", "net.tcp"); App.WriteLog("SetBinding", string.Format("Add binding end.")); //LSiteUMPPF.ApplicationDefaults.EnabledProtocols var app = LSiteUMPPF.Applications["/WCF1600"]; if (app != null) { app.EnabledProtocols = "http,net.tcp"; App.WriteLog("SetBinding", string.Format("Set protocol for WCF1600 end.")); } LServerManager.CommitChanges(); LServerManager.Dispose(); LServerManager = null; } catch (Exception ex) { LBoolReturn = false; AStrReturn = "010"; App.GStrCatchException = "SetSiteProtolPort()" + ex.ToString(); } return(LBoolReturn); }