在运行时获取IIS绑定

时间:2011-01-21 20:54:37

标签: asp.net iis binding runtime

我想知道如何使用ASP.NET在运行时获取当前站点(主机名,端口,IP地址)的IIS绑定设置。 .NET是否提供了获取这些信息的方法?

编辑:我需要一种方法来将http和https端口配置为在从http切换到https时重定向到正确的端口,如果使用其他端口80/443则从https返回到http。没有扩展权限,有没有办法做到这一点?

此致

4 个答案:

答案 0 :(得分:14)

实现这一目标的唯一方法(不是管理员)是使用Microsoft.Web.Administration。我刚写了一篇关于如何做到这一点的快速博客,请参阅:
http://blogs.msdn.com/b/carlosag/archive/2011/01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx

基本上,由于IIS具有我们称之为工作进程隔离的功能,因此可以从应用程序本身读取配置,而无需成为管理员。如果您使用ADSI,Metabase或任何其他方式,您将需要成为管理员。

答案 1 :(得分:3)

您应该可以通过使用System.DirectoryServices程序集访问IIS元数据库来完成此任务。

例如,您可以在此处列举这些网站中包含的所有网站和属性配置。

将此引用添加到您的项目中:

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

答案 2 :(得分:1)

答案 3 :(得分:0)

您可以使用以下代码获取绑定:

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;
}

以下代码可用于测试上述方法:

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/>");
}

使用的命名空间:

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

引用的程序集:Microsoft.Web.Administration

enter image description here

将上面的代码放入Sample.aspx中进行测试:

<%@ 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>

将为您提供如下输出:

enter image description here

相关问题