以编程方式将IIS主机标头添加到网站

bil*_*tom 5 c# asp.net iis hostheader

我想设置一个管理页面(ASP.NET/C#),可以将IIS主机标题添加到托管页面所在的网站.这可能吗?

我不想添加一个http标头 - 我想模仿手动进入IIS的动作,调出网站的属性,点击网站选项卡上的高级,以及高级网站识别屏幕和新的"身份"主机头值,ip地址和tcp端口.

bil*_*tom 2

这是一个关于以编程方式向站点添加另一个身份 RSS 的论坛

另外,这里有一篇关于如何在 IIS 中通过代码附加主机标头的文章:

以下示例将主机标头添加到 IIS 中的网站。这涉及更改 ServerBindings 属性。没有 Append 方法可用于将新的服务器绑定附加到此属性,因此需要做的是读取整个属性,然后将其与新数据一起重新添加回来。这就是下面的代码中所做的事情。ServerBindings 属性的数据类型为 MULTISZ,字符串格式为 IP:Port:Hostname。

请注意,此示例代码不执行任何错误检查。重要的是,每个 ServerBindings 条目都是唯一的,并且您(程序员)负责检查这一点(这意味着您需要循环遍历所有条目并检查要添加的内容是否唯一)。

using System.DirectoryServices;
using System;
 
public class IISAdmin
{
    /// <summary>
    /// Adds a host header value to a specified website. WARNING: NO ERROR CHECKING IS PERFORMED IN THIS EXAMPLE. 
    /// YOU ARE RESPONSIBLE FOR THAT EVERY ENTRY IS UNIQUE
    /// </summary>
    /// <param name="hostHeader">The host header. Must be in the form IP:Port:Hostname </param>
    /// <param name="websiteID">The ID of the website the host header should be added to </param>
    public static void AddHostHeader(string hostHeader, string websiteID)
    {
        
        DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID );
        try
        {                        
            //Get everything currently in the serverbindings propery. 
            PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
            
            //Add the new binding
            serverBindings.Add(hostHeader);
            
            //Create an object array and copy the content to this array
            Object [] newList = new Object[serverBindings.Count];
            serverBindings.CopyTo(newList, 0);
            
            //Write to metabase
            site.Properties["ServerBindings"].Value = newList;            
                        
            //Commit the changes
            site.CommitChanges();
                        
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        
    }
}
 
public class TestApp
{
    public static void Main(string[] args)
    {
        IISAdmin.AddHostHeader(":80:test.com", "1");
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不确定如何循环遍历标头值来执行提到的错误检查。