Recipe14.19.Determining the Application Mappings for ASP.NET Set Up on IIS


Recipe 14.19. Determining the Application Mappings for ASP.NET Set Up on IIS

Problem

You want to determine what application mappings are set up on a given web server for ASP.NET.

Solution

Use the System.DirectoryServices.DirectoryEntry class to examine the IIS metabase for the application mappings (also known as extension mappings) for the given web server. This is accomplished by creating a DirectoryEntry that talks to the IIS service provider for Active Directory and using it to locate the web server root node. The root node contains a ScriptMaps property that holds all extension mappings for the web server. Filter out the set of items that are redirected to the aspnet_isapi.dll (this ISAPI is the connector between IIS and the ASP.NET worker process) and the list of ASP.NET application mappings is created. The code for carrying out these operations is shown in Example 14-6.

Example 14-6. Determining the application settings for ASP.NET on a given web server

 // This is a case-sensitive entry in the metabase. // You'd think it was misspelled but you would be mistaken… const string WebServerSchema = "IIsWebServer"; // Set up to talk to the local IIS server. string server = "localhost"; // Create a dictionary entry for the IIS server with a fake // user and password. Credentials would have to be provided // if you are running as a regular user. using (DirectoryEntry w3svc =   new DirectoryEntry(string.Format("IIS://{0}/w3svc", server),     "Domain/UserCode", "Password")) {   // Can't talk to the metabase for some reason: bail.   if (w3svc != null)   {     foreach (DirectoryEntry site in w3svc.Children)     {       using (site)       {         if (site != null)         {          // Check all web servers on this box.          if (site.SchemaClassName == WebServerSchema)          {            // Get the metabase entry for this server.            string metabaseDir =              string.Format("/w3svc/{0}/ROOT", site.Name);            if (site.Children != null)            {              // Find the root directory for each server.              foreach (DirectoryEntry root in site.Children)              {                using (root)                {                  // Did we find the root dir for this site?                  if (root != null &&                      root.Name.Equals("ROOT", StringComparison.OrdinalIgnoreCase))                  {                    // Get the application mappings in the ScriptMaps property.                    if (root.Properties.Contains("ScriptMaps") == true)                    {                      // Write them out.                      PropertyValueCollection scriptMaps = root.Properties["ScriptMaps"];                      if (scriptMaps != null)                      {                          for (int i = 0; i < scriptMaps.Count; i++)                          {                            //.aspx,                            //C:\WINDOWS\Microsoft.NET\Framework\v2.0.50110\aspnet_isapi.     dll                            //,1,GET,HEAD,POST,DEBUG                            string[] mappingParts = scriptMaps[i].ToString().Split(',');                            // The ASP.NET redirector is implemented in the                            // aspnet_isapi.dll file so any extensions mapped to it                            // will be processed by ASP.NET.                            if (mappingParts[1].ToUpper().IndexOf("ASPNET_ISAPI") != -1)                            {                              // Write out details for the ASP.NET mappings.                              Console.WriteLine("Extension Mappings:");                              Console.WriteLine("\tMapped Extension: {0}",     mappingParts[0]);                              Console.WriteLine("\tHandler: {0}", mappingParts[1]);                              for (int j = 3; j < mappingParts.Length; j++)                                Console.WriteLine("\tHTTP VERB: {0}", mappingParts[j]);                            }                            else                            {                              // Write out those unmapped items.                              Console.WriteLine("Skipping {0} as it is not processed by     ASP.NET",                               mappingParts[0]);                            }                          }                        }                      }                    }                  }                }              }            }          }        }      }   } } 

Discussion

The metabase in IIS contains many properties of interest to web application developers, and it is easily explored using the MetaEdit tool available from Microsoft (search on support knowledge base article 232068). MetaEdit is like RegEdit for the IIS metabase and allows for easy exploration of the settings in IIS. As with RegEdit, much harm can be done while editing, so care should be taken when performing any modifications. All web and virtual directory settings are located in the appropriate web site folder on the server.

See Also

See Recipe 14.18; see the "ScriptMaps [IIS]" and "IIS Metabase Properties" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net