June 1, 2007

Writing IIS Host headers in C#

code for writing IIS host headers. You have to pass the host header value and the web site ID, and also you should provide the username and password.

public static void AddHostHeader(string hostHeader, string websiteID) {
DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID, "username", "password", AuthenticationTypes.None);
try {
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
serverBindings.Add(hostHeader);
Object[] newList = new Object[serverBindings.Count];

serverBindings.CopyTo(newList, 0);
site.Properties["ServerBindings"].Value = newList;
site.CommitChanges();
} catch (Exception e) {
throw e;
}
}

No comments: