2

如何在 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);

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

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

4

2 回答 2

3

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

appcmd set site /site.name "SiteName" /bindings.[protocol='https',bindingInformation='*:443:*'].BindingInformation:*:443:NewHostHeader
于 2015-08-28T12:52:47.937 回答
1

在审查了几个小时后,我回答了自己。我读到通过 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;
                            }
                        }                        
                    }
                }

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

于 2014-05-29T17:30:54.063 回答