开发者

Get IIS bindings at runtime

I wonder how to get the IIS binding settings of the current site (host name, port,开发者_如何学编程 IP address) at runtime using ASP.NET. Does .NET provide any way to get these information?

Edit: I need a way to get the http and https ports configured to redirect to the right port when switching from http to https, and back from https to http if other ports then 80/443 are used. Is there a way to do this without extended privileges?

Regards


The only way to achieve that (without being an administrator) is using Microsoft.Web.Administration. I just wrote a quick blog on how to do that, see:
http://blogs.msdn.com/b/carlosag/archive/2011/01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx

Basically since IIS has a feature we call Worker Process isolation it is possible to read the configuration from an Application itself without the need of being administrator. If you use ADSI, Metabase, or any other way, you will require being an administrator.


You should be able to accomplish this by accessing the IIS metabase, using the System.DirectoryServices assembly.

For example, here you can enumerate through all of your sites and property configurations contained within those sites.

Add this reference to your project:

using System.DirectoryServices

// Assuming your Server Id is 1, and you are connecting to your local IIS.
DirectoryEntry de = new DirectoryEntry(@"IIS://localhost/W3SVC/1/Root");
foreach (DirectoryEntry entry in de.Children)
{
   foreach (PropertyValueCollection property in entry.Properties)
   {
      Console.WriteLine("Name: {0}, Value {1}",property.PropertyName, property.Value);
   }
}


I think you're looking for metabase access:

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/43a51d34-7c81-413b-9727-ec9a19d0b428.mspx?mfr=true


You can use the following code to get the bindings:

public static IEnumerable<Binding> GetSiteBindings(Site site)
{
    BindingCollection bindings = site.Bindings;
    if (bindings != null)
    {
        foreach (Binding binding in bindings)
        {
            if (binding != null)
            {
                yield return binding;
            }
        }
    }

    yield return null;
}

Following code can be used to test the above method:

ServerManager mgr = new ServerManager();
foreach (Site s in mgr.Sites)
{
    Response.Write("Site: " + s);
    Response.Write("<br/>");

    var siteBindings = GetSiteBindings(s);
    if (siteBindings != null)
    {
        foreach (var binding in siteBindings)
        {
            if (binding != null)
            {
                var bindingInformation = binding.BindingInformation;
                var host = binding.Host;
                var endPoint = binding.EndPoint;

                Response.Write("Host: " + host + ", BindingInfo: " + bindingInformation + ", EndPoint: " + endPoint);
                Response.Write("<br/>");
            }
        }
    }

    Response.Write("----------------------------------");
    Response.Write("<br/>");
}

Namespaces used:

<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Microsoft.Web.Administration" %>

Assembly referred: Microsoft.Web.Administration

Get IIS bindings at runtime

Putting the above code in Sample.aspx to test it:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Microsoft.Web.Administration" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        ServerManager mgr = new ServerManager();
        foreach (Site s in mgr.Sites)
        {
            Response.Write("Site: " + s);
            Response.Write("<br/>");

            var siteBindings = GetSiteBindings(s);
            if (siteBindings != null)
            {
                foreach (var binding in siteBindings)
                {
                    if (binding != null)
                    {
                        var bindingInformation = binding.BindingInformation;
                        var host = binding.Host;
                        var endPoint = binding.EndPoint;

                        Response.Write("Host: " + host + ", BindingInfo: " + bindingInformation + ", EndPoint: " + endPoint);
                        Response.Write("<br/>");
                    }
                }
            }

            Response.Write("----------------------------------");
            Response.Write("<br/>");
        }
    }

    public static IEnumerable<Binding> GetSiteBindings(Site site)
    {
        BindingCollection bindings = site.Bindings;
        if (bindings != null)
        {
            foreach (Binding binding in bindings)
            {
                if (binding != null)
                {
                    yield return binding;
                }
            }
        }

        yield return null;
    }



</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="HtmlForm" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

Will give you output like this:

Get IIS bindings at runtime

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜