/// <summary> /// Assigns a certificate to a website binding /// </summary> /// <param name="siteInformation">Information identifying the website and binding to assign the certificate to</param> public static void AssignCertificateToSite(SiteInformation siteInformation) { if (siteInformation == null) { throw new ArgumentNullException("siteInformation"); } Binding binding = GetBinding(siteInformation); if (string.IsNullOrEmpty(siteInformation.CertificateHash)) { throw new CertificateException("Certificate hash is required when adding certificate."); } ConfigurationMethod configurationMethod = binding.Methods.FirstOrDefault(x => string.Equals(x.Name, "AddSslCertificate", StringComparison.InvariantCultureIgnoreCase)); if (configurationMethod == null) { throw new CertificateException("Unable to access the AddSslCertificate configuration method."); } ConfigurationMethodInstance configurationMethodInstance = configurationMethod.CreateInstance(); configurationMethodInstance.Input.SetAttributeValue("certificateHash", siteInformation.CertificateHash); configurationMethodInstance.Input.SetAttributeValue("certificateStoreName", siteInformation.CertificateStore); configurationMethodInstance.Execute(); }
/// <summary> /// 网站下的站点获取与操作 /// </summary> public static void ServerManagerMethods() { ServerManager iis = new ServerManager(); foreach (Microsoft.Web.Administration.Site item in iis.Sites) { //item.Start(); //item.Stop(); foreach (ConfigurationMethod itemMethod in item.Methods) { if (itemMethod.Name == "Stop") { ConfigurationMethodInstance methodInstance = itemMethod.CreateInstance(); methodInstance.Execute(); } Console.WriteLine(itemMethod.Name); } Console.WriteLine("网站:{0},状态:{1} ", item.Name, item.State); foreach (Application app in item.Applications) { Console.WriteLine("\t应用程序池:{0}", app.ApplicationPoolName); Console.WriteLine("\t 应用:{0}", app.Path); Console.WriteLine(); } } }
/// <summary> /// Marks a server as unhealthy /// </summary> /// <param name="farm">The name of the WebFarm</param> /// <param name="address">The address of the server</param> public void SetServerUnhealthy(string farm, string address) { ConfigurationElement arrElement = this.GetServerArr(farm, address); if (arrElement != null) { ConfigurationMethod method = arrElement.Methods["SetUnhealthy"]; ConfigurationMethodInstance instance = method.CreateInstance(); instance.Execute(); _Log.Information("Marking the server '{0}' as unhealthy.", address); } }
/// <summary> /// Marks a server as unavailable /// </summary> /// <param name="farm">The name of the WebFarm</param> /// <param name="address">The address of the server</param> public void SetServerUnavailable(string farm, string address) { ConfigurationElement arrElement = this.GetServerArr(farm, address); if (arrElement != null) { ConfigurationMethod method = arrElement.Methods["SetState"]; ConfigurationMethodInstance instance = method.CreateInstance(); instance.Input.Attributes[0].Value = 3; instance.Execute(); _Log.Information("Marking the server '{0}' as unavailable.", address); } }
public bool _IssueCommand(BLLPublishingPoint publishingPoint, PublishingPointCommand command) { bool tempresult = true; try { using (var serverManager = new ServerManager()) { Configuration appHost = serverManager.GetApplicationHostConfiguration(); ConfigurationSection liveStreamingConfig = appHost.GetSection(LIVESTREAMINGSECTION); if (liveStreamingConfig == null) { throw new Exception("Couldn't get to the live streaming section."); } ConfigurationMethodInstance instance = liveStreamingConfig.Methods[METHODGETPUBPOINTS].CreateInstance(); instance.Input[ATTR_SITENAME] = publishingPoint.SiteName; instance.Input[ATTR_VIRTUALPATH] = publishingPoint.Path; instance.Execute(); // Gets the PublishingPointCollection associated with the method output ConfigurationElement collection = instance.Output.GetCollection(); foreach (var item in collection.GetCollection()) { if (item.Attributes[ATTR_NAME].Value.ToString().Equals(publishingPoint.Name)) { var method = item.Methods[command.ToString()]; var methodInstance = method.CreateInstance(); methodInstance.Execute(); break; } } } } catch { tempresult = false; } return(tempresult); }
/// <summary> /// Unassigns a certificate to a website binding /// </summary> /// <param name="siteInformation">Information identifying the website and binding to assign the certificate to</param> public static void UnassignCertificateFromSite(SiteInformation siteInformation) { if (siteInformation == null) { throw new ArgumentNullException("siteInformation"); } Binding binding = GetBinding(siteInformation); ConfigurationMethod configurationMethod = binding.Methods.FirstOrDefault(x => string.Equals(x.Name, "RemoveSslCertificate", StringComparison.InvariantCultureIgnoreCase)); if (configurationMethod == null) { throw new CertificateException("Unable to access the RemoveSslCertificate configuration method."); } ConfigurationMethodInstance configurationMethodInstance = configurationMethod.CreateInstance(); configurationMethodInstance.Execute(); }
private static void Main() { using (ServerManager serverManager = new ServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); // Retrieve the sites collection. ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); // Locate a specific site. ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"mySite"); if (siteElement == null) { throw new InvalidOperationException("Element not found!"); } // Create an object for the ftpServer element. ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); // Create an instance of the FlushLog method. ConfigurationMethodInstance FlushLog = ftpServerElement.Methods["FlushLog"].CreateInstance(); // Execute the method to flush the logs for the FTP site. FlushLog.Execute(); } }
public StateExecutor(ConfigurationElement server) { _server = server; _method = _server.ChildElements["applicationRequestRouting"].Methods["SetState"].CreateInstance(); }
// Get all the publishing points on a server public static List <BLLPublishingPoint> GetPublishingPoints() { var retVal = new List <BLLPublishingPoint>(); using (var serverManager = new ServerManager()) { Configuration appHost = serverManager.GetApplicationHostConfiguration(); try { ConfigurationSection liveStreamingConfig = appHost.GetSection(LIVESTREAMINGSECTION); foreach (Site site in serverManager.Sites) { foreach (Application application in site.Applications) { try { ConfigurationMethodInstance instance = liveStreamingConfig.Methods[METHODGETPUBPOINTS].CreateInstance(); instance.Input[ATTR_SITENAME] = site.Name; instance.Input[ATTR_VIRTUALPATH] = application.Path; instance.Execute(); ConfigurationElement collection = instance.Output.GetCollection(); foreach (var item in collection.GetCollection()) { retVal.Add(new BLLPublishingPoint { SiteName = site.Name, Path = application.Path, Name = item.Attributes[ATTR_NAME].Value.ToString(), ArchiveState = (State)item.Attributes[ATTR_ARCHIVES].Value, FragmentState = (State)item.Attributes[ATTR_FRAGMENTS].Value, StreamState = (State)item.Attributes[ATTR_STREAMS].Value, PubState = (PublishingPointState)item.Attributes[ATTR_STATE].Value }); } } catch (COMException ce) { Debug.Print(ce.Message); } } } } catch (COMException ce) { Debug.Print(ce.Message); } } return(retVal); }
static void Main(string[] args) { try { string certHash = ""; string certStore = "MY"; X509Store certificateStore = new X509Store(StoreLocation.LocalMachine); certificateStore.Open(OpenFlags.ReadOnly); foreach (var certificate in certificateStore.Certificates) { bool isServerAuth = false; Console.WriteLine("[Info] " + certificate.GetCertHashString()); foreach (X509Extension extension in certificate.Extensions) { Console.WriteLine(extension.Format(true)); Console.WriteLine(extension.Oid.Value.ToString()); if (extension.Format(true).Contains("Server Authentication")) { isServerAuth = true; } if (certificate.Verify() && isServerAuth) { certHash = certificate.GetCertHashString(); } else if (certificate.Verify() && isServerAuth) { certHash = certificate.GetCertHashString(); } else if (certificate.Verify()) { certHash = certificate.GetCertHashString(); } } } certificateStore.Close(); ServerManager mgr = null; string server = null; // or remote machine name string siteName = "Default Web Site"; string bindingProtocol = "https"; string bindingInfo = "*:7443:"; if (String.IsNullOrEmpty(server)) { mgr = new ServerManager(); } else { mgr = ServerManager.OpenRemote(server); } Site site = mgr.Sites[siteName]; Binding binding = null; foreach (Binding b in site.Bindings) { Console.WriteLine(bindingInfo); if (b.Protocol.Equals(bindingProtocol) && b.BindingInformation.Equals(bindingInfo)) { binding = b; break; } } if (binding == null) { throw new Exception("Binding not found!"); } if (!String.IsNullOrEmpty(certHash)) { ConfigurationMethod method = binding.Methods["AddSslCertificate"]; if (method == null) { throw new Exception("Unable to access the AddSslCertificate configuration method"); } ConfigurationMethodInstance mi = method.CreateInstance(); mi.Input.SetAttributeValue("certificateHash", certHash); mi.Input.SetAttributeValue("certificateStoreName", certStore); mi.Execute(); Console.WriteLine("Certificate has been added: " + certHash); } else { Console.WriteLine("Certificate can not be found"); } } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); Console.Read(); Environment.Exit(-1); } Thread.Sleep(9000); }