Creating a new site on IIS using c# and ADSI

  • Thread starter Thread starter Corne Grotius
  • Start date Start date
C

Corne Grotius

Hiya,

I'm trying to create a new site on IIS 6.0 using ADSI and C# using the
following code:

DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName +
"/w3svc", Username, Password, AuthenticationTypes.Secure);
DirectoryEntries sites = W3SVC.Children;
DirectoryEntry newSite = sites.Add("1234","IIsWebServer"); //create
a new site
newSite.CommitChanges();

The user definitely has full admin priviledges, but I'm receiving the
following error:

System.UnauthorizedAccessException: Access is denied.
at System.DirectoryServices.Interop.IAds.SetInfo()
at System.DirectoryServices.DirectoryEntry.CommitChanges()
at UIAdmin.ConfigIIS.CreateNewWebSite(String SiteName) in
c:\inetpub\wwwroot\uiadmin\classes\configiis.cs:line 69

Other ADSI code work fine i.e.:

DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName +
"/w3svc", Username, Password, AuthenticationTypes.Secure);

foreach (DirectoryEntry Site in W3SVC.Children)
{
if (Site.SchemaClassName == WebServerSchema)
strSiteList += Site.Name + " - " +
Site.Properties["ServerComment"].Value.ToString() + "<br>";
}

Any ideas?
 
I am not sure why your credentials as you entered them are not
working, but I can give you some insight into your problem.

Calling DirectoryEntries.Add() causes the DirectoryEntry.Bind function
to be called, and the Bind function requires credentials that merely
looping through the data already sent to your box(the Children data)
does not require. Here is the call stack for DirectoryEntries.Add().

System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_IsContainer() at
System.DirectoryServices.DirectoryEntries.CheckIsContainer() at
System.DirectoryServices.DirectoryEntries.Add(String name, String
schemaClassName)

- Jessica
 
Back
Top