Running ASP.NET applications on Windows 7 machines

You might get a weird error when you publish your .NET site to a Windows 7 machine:

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Config Error This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".  

The fix for this requires a change to applicationHost.config, located at:

%windir%\system32\inetsrv\config\

Change the following section overrideModeDefault values from "Deny" to "Allow" for the system.webServer section in configSections:

<section name="**handlers**" overrideModeDefault="Deny" /> 
<section name="**modules**" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />

You might also get the following 404 error:

The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Module StaticFileModule
Notification ExecuteRequestHandler
Handler StaticFile
Error Code 0x80070032

You will get this error if your IIS isn't configured to run ASP.NET applications. Go to Control Panel > Programs > Turn Windows features on or off > Internet Information Services > World Wide Web Services > Application Development Features and select "ASP.NET" (which will automatically select all required dependencies).

You might then get this error:

Configuration Error 
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

You will get this error if your website is in .NET 4.0 but the app pool that your website is running under is targeting .NET 2.0. To fix it, go to IIS Manager > Application Pools and select your website's app pool (probably the DefaultAppPool). Click on "Basic Settings" in the "Actions" window to the right and select the right version in the ".NET Framework version" dropdown.

Finally, you mighet get this error:

Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

This can happen if ASP.NET 4.0 installatoin wasn't registered correctly; you can fix it by running the following command from the cmd prompt:

%windir%\Microsoft.NET\Framework\v[version number]\aspnet_regiis.exe -i

You should finally see your website.

Display DNS cache in Windows and Mac

Whenever you visit a website, your computer needs to retrieve the IP address for that address. To improve efficiency and speed, your computer will cache the name resolutions, so that if you visit Google a second time, you don't have to ask for the IP address again.

In Windows, if you want to view your DNS cache, the command is:

ipconfig /displaydns

In Mac (Snow Leopard), you can do the same thing by:

dscacheutil -cachedump -entries Host 

And you can flush your DNS cache with:

ipconfig /flushdns

in Windows and:

dscacheutil -flushcache

in Mac.

Navisphere: Flexible menu creation framework for ASP.NET MVC

I talked about Navisphere a menu-creation framework for ASP.NET MVC in a previous article. I finally got around to uploading the source.

View Navisphere on GitHub

Searching for a user in AD in .NET 3.5

.NET 3.5 brought with it the new System.DirectoryServices.AccountManagement namespace, which makes working with Active Directory so much easier. Let's take a look at how to find a user if you know their username (SAMAccountName):

private static UserPrincipal GetPrincipal(string name)
{
    var context = new PrincipalContext(
        ContextType.Domain, "yourdomain.com");

    var principal = UserPrincipal.FindByIdentity(
        context, IdentityType.SamAccountName, name);

    return principal;
}

Wow, three entire lines of code. I don't know how we'll ever convince anyone to switch from the old way of doing things. :-) How about getting the user's groups?

PrincipalSearchResult<Principal> GetGroups(UserPrincipal principal)
{
    return principal.GetAuthorizationGroups();  
}

How about you just want to dump properties about that principal?

private static IDictionary<string, string> GetProperties(UserPrincipal principal)
{
    var properties = new Dictionary<string, string>();

    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;

    var allProperties = directoryEntry.Properties;

    foreach (var property in allProperties.PropertyNames)
    {
        var propertyName = property.ToString();

        var value = string.Empty;

        if (allProperties[propertyName] != null && allProperties[propertyName].Count > 0)
        {
            foreach (var val in allProperties[propertyName])
            {
                if (val != null)
                {
                    value += ", " + val.ToString();
                }
            }
        }

        properties.Add(property.ToString(), value); 
    }

    return properties;
}

1 of 1 pages

On the Side