Get the ASP .NET authentication mode in Web.config

I’ve recently had to implement an HTTP module in an ASP .NET application that would have different behaviours depending on the authentication mode defined in the authentication section of the Web.config (i.e. Forms, Windows, Passport, None).

Turns out ASP .NET doesn’t surface these settings directly, but I was able to get this information through ConfigurationManager, by getting a direct reference to the Web.config authentication section, and then read the value I wanted:

var authSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
if (authSection.Mode == AuthenticationMode.Windows)
{
    // ...
}

The nice thing about this approach is that we get an instance of the AuthenticationSection class that allows strongly-typed access to the attributes of the section, rather than having to parse the XML of the Web.config to get the same information.

This entry was posted in .NET, ASP .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>