Detecting Web.Config Authentication Mode
Solution 1
Try Context.User.Identity.AuthenticationType
Go for PB's answer folks
Solution 2
The mode property from the authenticationsection: AuthenticationSection.Mode Property (System.Web.Configuration). And you can even modify it.
// Get the current Mode property.
AuthenticationMode currentMode =
authenticationSection.Mode;
// Set the Mode property to Windows.
authenticationSection.Mode =
AuthenticationMode.Windows;
This article describes how to get a reference to the AuthenticationSection.
Solution 3
Import the System.Web.Configuration
namespace and do something like:
var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
//do something
}
Solution 4
You can also get the authentication mode by using the static ConfigurationManager
class to get the section and then the enum AuthenticationMode
.
AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;
The difference between WebConfigurationManager and ConfigurationManager
If you want to retrieve the name of the constant in the specified enumeration you can do this by using the Enum.GetName(Type, Object)
method
Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"
GateKiller
I am a Web Developer working in the UK, developing web applications in ASP.NET C#. Click me
Updated on September 15, 2021Comments
-
GateKiller over 2 years
Say I have the following web.config:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <authentication mode="Windows"></authentication> </system.web> </configuration>
Using ASP.NET C#, how can I detect the Mode value of the Authentication tag?
-
GateKiller over 15 yearsI have accepted your answer because yours was the quickest and worked :)
-
Joe over 15 yearsNo this doesn't work in the general case. An ASP.NET app inherits settings from machine.Config and from all other web.config files higher in the virtual directory tree: see msdn.microsoft.com/en-us/library/ms178685.aspx Your technique only looks at the lowest web.config file.
-
Joe over 15 yearsThis is wrong. In the general case IIdentity.AuthenticationType can contain any string, which may not necessarily match the authentication mode set in web.config. I'd use the solution from @pb.
-
Anthony Mason about 8 yearsXPath is not something that should be used to parse the configuration by any means. Utilizing the libraries provided by MS is a far more efficient and maintainable approach. The comment above is a perfect example of why not to use it as well as the fact that not all platforms will have necessarily utilize the configuration documents for authentication or other settings; another valid case is if the location of the authentication type is changed, then you must replace a hardcoded string, recompile, then redistribute.
-
Michael Freidgeim about 7 yearsYou should use application root "~" instead of site root "/", but better to call WebConfigurationManager.GetSection("system.web/authentication") directly