2

I want to add a host header to a website which is working on IIS7 through a web application (asp.net 4.0 / C#).There are some examples in internet,but i guess most of them dont work on iis7. (note:the web application is being hosted in same server so i guess there wont be a security problem while changing iis configurations)

Any help is appreciated,thanks

slayer35
  • 604
  • 2
  • 8
  • 26
  • this question may help you http://stackoverflow.com/questions/5121383/iis-7-0-vs-7-5-site-microsoft-web-administration-site-bindingcollection – Claudio Redi Feb 11 '12 at 15:16
  • I think you are not clear enough. Can you provide more details? Do you want add/change binding? – Tomek Feb 11 '12 at 15:17

2 Answers2

3

I found this solution and it works for me.This is a little function with couple parameters,just you have to find the id of yourwebsite in your iss configuration.After that you have to give the ip adress of the server (iis),and port number,and hostname to the function and it will add a hostheader by using the parameters you entered.For example

AddHostHeader(2, "127.0.0.1:81", 81, "newsHostHeader");

  static void AddHostHeader(int? websiteID, string ipAddress, int? port, string hostname)
    {
        using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID.ToString()))
        {
            var bindings = directoryEntry.Properties["ServerBindings"];
            var header = string.Format("{0}:{1}:{2}", ipAddress, port, hostname);
            if (bindings.Contains(header))
              throw new InvalidOperationException("Host Header already exists!");
            bindings.Add(header);
            directoryEntry.CommitChanges();
        }
    }

(note:do not forget to add to the page using System.DirectoryServices; using Microsoft.Web.Administration; )

slayer35
  • 604
  • 2
  • 8
  • 26
  • throws up at System.DirectoryServices.DirectoryEntry.Bind on Windows 8 IIS 7.5 System.Runtime.InteropServices.COMException occurred – noob Mama Mar 09 '13 at 08:29
  • Maybe you need to check some credentials settings,it sounds like IUSR user is not allowed to add header to the website in your IIS. – slayer35 Mar 09 '13 at 15:00
  • err.. i eventualy used this to get it done http://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding – noob Mama Mar 09 '13 at 16:44
0

The above solution didn't quite work with IIS7.5 for me. I eventually had to do this http://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding

noob Mama
  • 267
  • 8
  • 28