Home > Uncategorized > Modify #IIS bindings from c#

Modify #IIS bindings from c#

iis

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>”);

}
}

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: