在运行时获取 IIS 绑定

发布于 2024-10-12 23:51:27 字数 223 浏览 2 评论 0原文

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

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

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

燃情 2024-10-19 23:51:27

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

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

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:
https://web.archive.org/web/20150722034236/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.

爱你不解释 2024-10-19 23:51:27

您应该能够通过使用 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);
   }
}

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);
   }
}
白色秋天 2024-10-19 23:51:27

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

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

在此处输入图像描述

将上述代码放入 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>

会给你这样的输出:

在此处输入图像描述

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

enter image description here

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:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文