|
How To Enumerate Web Application Directories in IIS Using DirectoryServices
In our previous ADSI articles we have been setting up some basics on how to use
System.DirectoryServices namespace to get some information from Active
Directory. We used LDAP provider to extract the
information. But that is not the only provider that can be used with DirectoryServices
classes. The DirectoryEntry class can be used with other providers
like WinNT, IIS and Novel Netware(NDS, NWCOMPAT).
In this article we will demonstrate how IIS provider can be used
to get complete list of web applications in IIS.
How To Do It
The procedure to accomplish is different from the one used for enumeration of
Active Directory Users and Groups. The reason is the DirectorySearcher
class can only be used with LDAP provider. This means that we
don't have the luxury of specifying SearchFilter and invoke the
search. Now the work is accomlished by DirectoryEntry class. You
will specify the ADsPath for the web server and the object you are
looking for and then call Children property to get the list of
children items.
A general ADsPath can be specified as IIS://MachineName/W3SVC/N/Root.
When we specify this path, it returns list of all IIsWebVirtualDir
and IIsWebDirectory containers. Since we are only interested in
the web applications, check SchemaClassName value for each child
DirectoryEntry object returned by Children property.
The objects whose class name matches "IIsWebDirectory" is added to the StringCollection
for later display.
public StringCollection GetIISVirtualFolders(string strServer)
{
StringCollection strColl = null;
DirectoryEntry obDirEntry = null;
DirectoryEntries obDirEntList = null;
try
{
obDirEntry = new DirectoryEntry("IIS://" + strServer + "/W3svc/1/Root");
obDirEntList= obDirEntry.Children;
// Process each child entry and add the name of virtual folder
// to string collection.
strColl = new StringCollection();
foreach(DirectoryEntry objChildDE in obDirEntList)
{
ProcessDEForIISVFolder(objChildDE, strColl);
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
return null;
}
return strColl;
}
private void ProcessDEForIISVFolder(DirectoryEntry ob, StringCollection strColl)
{
try
{
// Check if the schema class is IIsWebVirtualDir or not.
if (0 == String.Compare( ob.SchemaClassName, "IIsWebDirectory"))
{
strColl.Add(ob.Name);
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
Platforms Tested
We have tested the included project on following platforms
-
Windows 2000 Adv. Server
-
Windows XP Professional
-
Windows .NET Enterprise Server (Beta 3)
For any comments or suggestions, feel free to contact us at
softomatix@pardesiservices.com
|