Add IIS host header to website programmatically

I would like to set up an admin page (ASP.NET/C#) that can add IIS host headers to the website that hosts the admin page. Is it possible?

I don’t want to add an http header - I want to mimic the action of logging into IIS manually by invoking website properties, dragging and dropping the advanced ones on the website tab, as well as on the extended website identification screen and the new β€œidentity” with the host header value, IP address and TCP port.

+4
source share
1 answer

Here's a forum on adding another identification to the Programmatically RSS site

In addition, here is an article on how to add host header code in IIS :

The following example adds the site header to a website in IIS. This is due to a change in the ServerBindings property. There is no Append method that can be used to add a new server binding to this property, so you need to do all the reading of the entire property and then add it again with the new data. This is what is done in the code below. The ServerBindings property data type is MULTISZ, and the string format is IP: Port: Hostname.

Please note that this sample code does not perform error checking. It is important that each ServerBindings record is unique, and you, the programmer, are responsible for checking it (this means that you need to go through all the records and verify that what needs to be added is unique).

using System.DirectoryServices; using System; public class IISAdmin { /// <summary> /// Adds a host header value to a specified website. WARNING: NO ERROR CHECKING IS PERFORMED IN THIS EXAMPLE. /// YOU ARE RESPONSIBLE FOR THAT EVERY ENTRY IS UNIQUE /// </summary> /// <param name="hostHeader">The host header. Must be in the form IP:Port:Hostname </param> /// <param name="websiteID">The ID of the website the host header should be added to </param> public static void AddHostHeader(string hostHeader, string websiteID) { DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID ); try { //Get everything currently in the serverbindings propery. PropertyValueCollection serverBindings = site.Properties["ServerBindings"]; //Add the new binding serverBindings.Add(hostHeader); //Create an object array and copy the content to this array Object [] newList = new Object[serverBindings.Count]; serverBindings.CopyTo(newList, 0); //Write to metabase site.Properties["ServerBindings"].Value = newList; //Commit the changes site.CommitChanges(); } catch (Exception e) { Console.WriteLine(e); } } } public class TestApp { public static void Main(string[] args) { IISAdmin.AddHostHeader(":80:test.com", "1"); } } 

But I'm not sure how to iterate over the values ​​of the headers to check for the indicated error.

+2
source

Source: https://habr.com/ru/post/1277640/


All Articles