Get IIS bindings at runtime without being an Administrator

Today there was a question in StackOverflow asking whether it was possible to read the IIS binding information such as Port and Protocols from the ASP.NET application itself to try to handle redirects from HTTP to HTTPS in a way that was reliable without worrying about using different ports than 80/443.

Turns out this is possible in the context of the IIS worker process by using Microsoft.Web.Administration.

The following function will take care of that by reading the Worker Process isolated configuration file and find the HTTP based bindings.

    private static IEnumerable<KeyValuePair<string, string>> GetBindings(HttpContext context) {
// Get the Site name 
        string siteName = System.Web.Hosting.HostingEnvironment.SiteName;

// Get the sites section from the AppPool.config
        Microsoft.Web.Administration.ConfigurationSection sitesSection =
Microsoft.Web.Administration.WebConfigurationManager.GetSection(null, null, "system.applicationHost/sites");
foreach (Microsoft.Web.Administration.ConfigurationElement site in sitesSection.GetCollection()) {
// Find the right Site
            if (String.Equals((string)site["name"], siteName, StringComparison.OrdinalIgnoreCase)) {

// For each binding see if they are http based and return the port and protocol
                foreach (Microsoft.Web.Administration.ConfigurationElement binding in site.GetCollection("bindings")) {
string protocol = (string)binding["protocol"];
string bindingInfo = (string)binding["bindingInformation"];

if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase)) {
string[] parts = bindingInfo.Split(':');
if (parts.Length == 3) {
string port = parts[1];
yield return new KeyValuePair<string, string>(protocol, port);


If you want to try it, you could use the following page, just save it as test.aspx and add the function above, the result is a simple table that shows the protocol and port to be used:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e) {
Response.Write("<table border='1'>");
foreach (KeyValuePair<string, string> binding in GetBindings(this.Context)) {

Also, you will need to add Microsoft.Web.Administration to your compilation assemblies inside the web.config for it to work:

<?xml version="1.0"?>
<compilation debug="true">
<add assembly="Microsoft.Web.Administration, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
Comments (4)

Cancel reply

  1. Andrew says:

    Is it possible to add an assembly line into the web.config programmatically using the Microsoft.Web.Administration?

  2. CarlosAg says:

    @Andrew, yes you can the following code was generated using Configuration Editor:

    using System;

    using System.Text;

    using Microsoft.Web.Administration;

    internal static class Sample {

       private static void Main() {

           using(ServerManager serverManager = new ServerManager()) {

               Configuration config = serverManager.GetWebConfiguration("Default Web Site");

               ConfigurationSection compilationSection = config.GetSection("system.web/compilation");

               ConfigurationElementCollection assembliesCollection = compilationSection.GetCollection("assemblies");

               ConfigurationElement addElement = assembliesCollection.CreateElement("add");

               addElement["assembly"] = @"MyAssembly";






  3. Hi Carlos,

    I try to get iis settings (application pool, physical path, site bindings, connections …) from c #, I want to show data in a gridview

    do you have any idea how I can achieve this?

    Thanks a lot.


  4. Metal Detector says:

    Thanks for the post Carlos! When I implemented the GetBindings code, I received this error:
    “The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)”
    on the line:
    Microsoft.Web.Administration.ConfigurationSection sitesSection = Microsoft.Web.Administration.WebConfigurationManager.GetSection(null, null, “system.applicationHost/sites”);

    Any idea why this could be happening? Thanks!

Skip to main content