Modify #IIS bindings from c#
If you wanted to determine what host name an IIS website responded to dynamically, then you can use Microsoft.Web.Administration to do this. You need to run your application pool under localSystem privileges, so it’s perhaps not advisable to do on a public-facing website. You can find the DLL at Windows\system32\inetsrv
This little POC page, lists all sites on my local dev IIS, and then adds a new random binding to a site named “localhost90”
using (ServerManager serverManager = new ServerManager())
{
foreach (var site in serverManager.Sites)
{
Response.Write(site.Name);// add a random binding
if (site.Name == “localhost90”)
{
Random rnd = new Random();
int intRandomPort = rnd.Next(100, 1000);
Binding binding = site.Bindings.CreateElement(“binding”);
binding[“protocol”] = “http”;
binding[“bindingInformation”] = “:” + intRandomPort + “:”;
site.Bindings.Add(binding);
serverManager.CommitChanges();
}Response.Write(“<br>”);
Response.Write(“<ul>”);
foreach (var binding in site.Bindings)
{
Response.Write(“<li>” + binding.EndPoint + “</li>”);
}
Response.Write(“</ul>”);}
}