c# - 以编程方式将 IIS 主机 header 添加到网站

标签 c# asp.net iis hostheader

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

我不想添加 http header - 我想模仿手动进入 IIS 的操作,调出网站的属性,单击网站选项卡上的高级,然后在高级网站识别屏幕和一个新的“identity”,带有主机 header 值、IP 地址和 TCP 端口。

最佳答案

这是关于 Adding Another Identity To A Site Programmatically RSS 的论坛

另外,这里有一篇关于如何 Append a host header by code in IIS 的文章:

The following example adds a host header to a website in IIS. This involves changing the ServerBindings property. There is no Append method that can be used to append a new serverbinding to this property, so what needs to be done is read the entire property and then add it back again, together with the new data. This is what is done in the code below. The ServerBindings property's data type is MULTISZ, and the string format is IP:Port:Hostname.

Note that this example code does not do any error checking. It is important that every ServerBindings entry is unique, and you - the programmer - is responsible for checking this (which means that you need to loop thru all entries and check if what is about to be added is unique).

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

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

关于c# - 以编程方式将 IIS 主机 header 添加到网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/190043/

相关文章:

c# - 具有非标准fmt标题的WAVE文件

jquery - 由于 jquery,Listview ItemCommand 没有触发

c# - HttpClient 调用 Windows 身份验证 Api Controller 方法...但没有 WindowsIdentity 出现

wordpress - IIS 和 Wordpress 反向代理的 HTTPS (SSL) 问题

c# - 在 C# 中派生静态类

c# - func<T,T>作为函数参数的action用<T>,body怎么写?

c# - 需要 Windows-Mobile 上的签名示例(油漆)

c# - 在 ASP.NET 中异步发送电子邮件的正确方法...(我做对了吗?)

HTML5 视频循环不适用于 Chrome (Sitefinity CMS)

angularjs - angular.service 调用错误:net::ERR_INSECURE_RESPONSE