【问题标题】:Edit binding IIS 8 by C# or by appcmd通过 C# 或 appcmd 编辑绑定 IIS 8
【发布时间】:2014-05-29 17:24:03
【问题描述】:

如何在 IIS 8 (Windows 8) 中修改已配置站点中的现有绑定?我正在尝试通过命令提示符执行此操作。

我可以通过以管理员模式运行的命令提示符添加新的绑定:

> C:\Windows\System32\inetsrv>appcmd set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']

在命令提示符下我使用:

> C:\Windows\System32\inetsrv>appcmd set site "test" /?

要查看 SET Binding 选项,并且不存在作为“SET Binding BY BINDING ID”的命令。

通过我使用的 C# 代码:

string windir = Environment.GetEnvironmentVariable("windir");    
string comando = windir +"\\System32\\inetsrv\\appcmd.exe set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']";

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + comando);

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;                
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

string result = proc.StandardOutput.ReadToEnd();

Console.WriteLine(result);
Debug.WriteLine(result);

我得到错误:“由于权限不足,无法读取配置文件”

但是我不能通过命令修改。而且我无法通过代码为下一步创建绑定尝试修改它。

【问题讨论】:

    标签: c# iis appcmd


    【解决方案1】:

    即使问题很老,我也会回答这个问题,因为我遇到了同样的问题,并使用 APPCMD 工具找到了以下可行的解决方案。此示例说明如何将 Host-Header 添加到现有 SSL-Binding。

    appcmd set site /site.name "SiteName" /bindings.[protocol='https',bindingInformation='*:443:*'].BindingInformation:*:443:NewHostHeader
    

    【讨论】:

    • 今天我需要稍微修改一下上面的CMD。必须删除现有绑定中的一颗星才能工作...appcmd set site /site.name "SiteName" /bindings.[protocol='https',bindingInformation='*:443:'].BindingInformation:*:443:NewHostHeader
    【解决方案2】:

    在审查了几个小时后,我回答了自己。我读到通过 c# 代码使用 appcmd 非常复杂,因为权限。最后我使用了 ServerManager 类,首先我在我的项目中引用了这个 dll:

    C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

    然后我使用代码来操作 AppPools、站点或绑定。绑定没有 ID,那么您可以使用 HashTable(在我的情况下为“bindingNameBase”)来维护具有主机名的键(在我的项目/问题中是唯一的)所以它:

    public void EditBinding(int id, SiteBinding siteBinding, string newKeyName)
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    if (serverManager.Sites == null)
                        return;
    
                    for (int i = 0; i < serverManager.Sites.Count; i++)
                    {
                        if (serverManager.Sites[i].Id == id)
                        {
                            Microsoft.Web.Administration.BindingCollection bindingCollection = serverManager.Sites[i].Bindings;
    
                            // se elimina el binding
                            Microsoft.Web.Administration.Binding bindingTmp = null;
                            for (int j = 0; j < bindingCollection.Count; j++)
                            {
                                if (bindingCollection[j].Host == bindingNameBase[newKeyName].ToString())
                                {
                                    bindingTmp = bindingCollection[j];                                
                                    break;
                                }
                            }
    
                            if (bindingTmp != null)
                            {
                                bindingCollection.Remove(bindingTmp);
    
                                //se crea de nuevo
                                Microsoft.Web.Administration.Binding binding = serverManager.Sites[i].Bindings.CreateElement("binding");
                                binding["protocol"] = siteBinding.Protocol;
                                binding["bindingInformation"] = string.Format(@"{0}:{1}:{2}", siteBinding.IPAddress, siteBinding.Port.ToString(), siteBinding.HostName);
    
                                bool existe = false;
                                for (int j = 0; j < bindingCollection.Count; j++)
                                {
                                    if (bindingCollection[j].Host == siteBinding.HostName)
                                    {
                                        existe = true;
                                        break;
                                    }
                                }
    
                                if (existe == false)
                                {
                                    bindingCollection.Add(binding);
                                    serverManager.CommitChanges();
    
                                    bindingNameBase[newKeyName] = siteBinding.HostName;
                                }
                            }                        
                        }
                    }
    

    站点必须使用具有正确身份的池,否则您的权限有问题。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多