Recipe14.18.Checking out a Web Server s Custom Error Pages


Recipe 14.18. Checking out a Web Server's Custom Error Pages

Problem

You have an application that needs to know what custom error pages are set up for the various HTTP error return codes on a given IIS server.

Solution

Use the System.DirectoryServices.DirectoryEntry class to talk to the Internet Information Server (IIS) metabase to find out which custom error pages are set up. The metabase holds the configuration information for the web server. DirectoryEntry uses the Active Directory IIS service provider to communicate with the metabase by specifying the "IIS" scheme in the constructor for the DirectoryEntry.

 // 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")) { 

Once the connection is established, the web server schema entry is specified to show where the IIS settings are kept (IIsWebServer). The DirectoryEntry has a property that allows access to its children (Children) and the SchemaClassName is checked for each entry to see if it is in the web server settings section. Once the web server settings are found, the web root node is located, and from there the HttpErrors property is retrieved. HttpErrors is a comma-delimited string that indicates the HTTP error code, the HTTP suberror code, the message type, and the path to the HTML file to serve when this error occurs. Using the Split method to break this into a string array allows the code to access the individual values and write them out. The code for carrying out these operations is shown in Example 14-5.

Example 14-5. Finding custom error pages

    // Can't talk to the metabase for some reason: bail.    if (w3svc != null)    {        foreach (DirectoryEntry site in w3svc.Children)        {            if (site != null)            {                using (site)                {                    // 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 HttpErrors.                                         if (root.Properties.Contains("HttpErrors") == true)                                         {                                             // Write them out.                                            PropertyValueCollection httpErrors = root.Properties["HttpErrors"];                                            if (httpErrors != null)                                            {                                                for (int i = 0; i < httpErrors.Count; i++)                                                { //400,*,FILE,C:\WINDOWS\help\iisHelp\common\400.htm                                                string[] errorParts = httpErrors[i]. ToString().Split(',');                                                Console.WriteLine("Error Mapping Entry:");                                                Console.WriteLine("\tHTTP error code: {0}", errorParts[0]);                                                Console.WriteLine("\tHTTP sub-error code: {0}", errorParts[1]);                                                Console.WriteLine("\tMessage Type: {0}", errorParts[2]);                                                Console.WriteLine("\tPath to error HTML file: {0}", errorParts[3]);                                                }                                            }                                        }                                    }                                }                            }                        }                    }                }            }        }    } // End using initial DirectoryEntry. 

At this point, an application could cache these settings for mapping its own error results, or it could dynamically modify the error pages to provide customized content. The important thing to take away is that the settings information for the web server is readily available to all applications with a bit of coding.

Discussion

System.DirectoryServices.DirectoryEntry is usually used for Active Directory programming, but it is able to use any of the providers that are available for Active Directory as well. This approach allows code to examine the IIS metabase for both the older style IIS 5.x metabases as well as the newer IIS 6.0 metabase that ships with Windows Server 2003.I

See Also

See Recipe 14.19; see the "HttpErrors [IIS]," "IIS Metabase Properties," and "DirectoryEntry Class" 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