Reputation: 2056
How do you modify an existing binding in a configured Site in IIS 8 (Windows 8)? I'm trying to do it by the command prompt.
I can add a new binding by command prompt running as administrator mode:
> C:\Windows\System32\inetsrv>appcmd set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']
In command prompt I use:
> C:\Windows\System32\inetsrv>appcmd set site "test" /?
To see the SET Binding options, and are not exist a command as "SET Binding BY BINDING ID".
By C# code I use:
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);
And I get the Error: "Cannot read configuration file due to insufficient permissions"
But I can't modify by command. And I can't create binding by code for next step try modify it.
Upvotes: 2
Views: 4095
Reputation: 226
Even if the question is old, i will answer this because i had the same issue and found the following working solution with the APPCMD tool. This Example shows how to add a Host-Header to an existing SSL-Binding.
appcmd set site /site.name "SiteName" /bindings.[protocol='https',bindingInformation='*:443:*'].BindingInformation:*:443:NewHostHeader
Upvotes: 3
Reputation: 2056
I answer myself, after reviewing the issue for hours. I read that using appcmd by c# code is very complicated because permissions. Finally I used the ServerManager class, first I reference this dll in my project:
C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
then I use the code to manipulate the AppPools, Sites or Bindings. The binding not have a ID then you can use a HashTable, in my case "bindingNameBase", to mantain keys with the hostname (are uniques in my project/problem) so It:
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;
}
}
}
}
The Site must by use a Pool with correct identity, or you have problems with permissions.
Upvotes: 1