从 IIS 检索绑定信息

Retrieving binding information from IIS

在我的 IIS 设置中有两个站点:默认网站,其下有许多 Web 应用程序;和另一个网站“ABC”。

我试图从默认网站中的 Web 应用程序获取 ABC 的绑定信息(协议和端口),但我遇到了麻烦!我确实获得了我需要的信息,但不是来自我需要的网站。

据我了解,信息来自位于 C:\Windows\System32\inetsrv\config 的“applicationHost.config”。我检查了这个文件,我可以看到默认网站和 ABC 作为网站。

这是我用来获取端口和协议的:

private Dictionary<string, string> GetBindings()
{
    // Get the Site name 
    string siteName = System.Web.Hosting.HostingEnvironment.SiteName;
    //string siteName = "ABC";  //  This does not return any results, but this is what I need info on

    Dictionary<string, string> ret = new Dictionary<string, string>();

    // 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];
                        ret.Add(protocol, port);
                    }
                }
            }
        }
    }
    return ret;
}

applicationHost.config 包含:

    <sites>
        <site name="Default Web Site" id="1" serverAutoStart="true">
            <application path="/">
                <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
            </application>
            <application path="/AHSTest" applicationPool="DefaultAppPool">
                <virtualDirectory path="/" physicalPath="E:\inetpub\wwwroot\AHSTest" />
            </application>
            <application path="/BCA" applicationPool="DefaultAppPool">
                <virtualDirectory path="/" physicalPath="E:\inetpub\wwwroot\BCA" />
            </application>

<!-- This is the web application my code is in  -->

            <application path="/ABCTools" applicationPool="ABCTools">
                <virtualDirectory path="/" physicalPath="E:\inetpub\wwwroot\ABCTools" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:80:" />
                <binding protocol="net.tcp" bindingInformation="808:*" />
                <binding protocol="net.msmq" bindingInformation="localhost" />
                <binding protocol="msmq.formatname" bindingInformation="localhost" />
                <binding protocol="net.pipe" bindingInformation="*" />
            </bindings>
        </site>

<!-- This is the site I need binding info on: http, port 7070  -->

        <site name="ABC" id="2" serverAutoStart="true">
            <application path="/" applicationPool="ABC">
                <virtualDirectory path="/" physicalPath="E:\inetpub\wwwroot\ABCWS" />
            </application>
            <application path="/ABCAPI/Services" applicationPool="ABC">
                <virtualDirectory path="/" physicalPath="E:\inetpub\wwwroot\ABCWS\ABCAPI\Services" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:7070:" />
            </bindings>
        </site>
        <siteDefaults>
            <logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles" />
            <traceFailedRequestsLogging directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles" />
        </siteDefaults>
        <applicationDefaults applicationPool="DefaultAppPool" />
        <virtualDirectoryDefaults allowSubDirConfig="true" />
    </sites>

更新 - 修复

public static Dictionary<string, int> GetBindings(string siteName)  // "ABC"
{
    Dictionary<string, int> ret = new Dictionary<string, int>();
    
    var lServerManager = new ServerManager();
    SiteCollection scSites = lServerManager.Sites;
    var abcSite = scSites[siteName];

    if (null != abcSite)
    {
        foreach (var binding in abcSite.Bindings)
        {
            int port = binding.EndPoint.Port;
            ret.Add(binding.Protocol, port);
        }
    }
    return ret;
}

我无法理解您的问题,但是 Microsoft docs 上的示例代码展示了如何获取和显示每个站点的绑定。

更新:

// Get the site collection on this server.
ServerManager serverManager = new ServerManager();
SiteCollection siteCollection = serverManager.Sites;
string collectiondisplay = null;
collectiondisplay = "There are " + siteCollection.Count.ToString() + " sites:\n\n";
string sitedisplay = null;
foreach (Site site in siteCollection)
{
    sitedisplay = sitedisplay + site.Name + ": ID= " + site.Id + "\n";
    // Display each property of each bindings.
    string bindingdisplay = null;
    foreach (Microsoft.Web.Administration.Binding binding in site.Bindings)
    {
        bindingdisplay = bindingdisplay + "  Binding:\n   BindingInformation: " + 
            binding.BindingInformation;
        if (binding.Protocol == "https")
        {
             // There is a CertificateHash and  
             // CertificateStoreName for the https protocol only.
            bindingdisplay = bindingdisplay + "\n   CertificateHash: " + 
                binding.CertificateHash + ": ";
            // Display the hash.
            foreach (System.Byte certhashbyte in binding.CertificateHash)
            {
                bindingdisplay = bindingdisplay + certhashbyte.ToString() + " ";
            }
            bindingdisplay = bindingdisplay + "\n   CertificateStoreName: " + 
                binding.CertificateStoreName;
        }
        bindingdisplay = bindingdisplay + "\n   EndPoint: " + binding.EndPoint;
        bindingdisplay = bindingdisplay + "\n   Host: " + binding.Host;
        bindingdisplay = bindingdisplay + "\n   IsIPPortHostBinding: " + binding.IsIPPortHostBinding;
        bindingdisplay = bindingdisplay + "\n   Protocol: " + binding.Protocol;
        bindingdisplay = bindingdisplay + "\n   ToString: " + binding.ToString();
        bindingdisplay = bindingdisplay + "\n   UseDsMapper: " + binding.UseDsMapper + "\n\n";
    }
    sitedisplay = sitedisplay + bindingdisplay;
}
collectiondisplay = collectiondisplay + sitedisplay + "\n";
testLabel.Text = collectiondisplay;