15

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
  • 74,180
  • 73
  • 171
  • 204

6 Answers6

29

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.

Paul van Brenk
  • 7,450
  • 2
  • 33
  • 38
12

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
}
bkaid
  • 51,465
  • 22
  • 112
  • 128
  • You should use application root "~" instead of site root "/", but better to call WebConfigurationManager.GetSection("system.web/authentication") directly – Michael Freidgeim Mar 01 '17 at 07:10
5

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"
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
clD
  • 2,523
  • 2
  • 22
  • 38
4

Try Context.User.Identity.AuthenticationType

Go for PB's answer folks

abatishchev
  • 98,240
  • 88
  • 296
  • 433
redsquare
  • 78,161
  • 20
  • 151
  • 159
  • I have accepted your answer because yours was the quickest and worked :) – GateKiller Sep 18 '08 at 12:17
  • 1
    This 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. – Joe Sep 18 '08 at 13:48
0

In ASP.Net Core you can use this:

public Startup(IHostingEnvironment env, IConfiguration config)
{
    var enabledAuthTypes = config["IIS_HTTPAUTH"].Split(';').Where(l => !String.IsNullOrWhiteSpace(l)).ToList();
}
SZL
  • 805
  • 8
  • 12
-3

use an xpath query //configuration/system.web/authentication[mode] ?

protected void Page_Load(object sender, EventArgs e)
{
 XmlDocument config = new XmlDocument();
 config.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
 XmlNode node = config.SelectSingleNode("//configuration/system.web/authentication");
 this.Label1.Text = node.Attributes["mode"].Value;
}
cgreeno
  • 31,943
  • 7
  • 66
  • 87
timvw
  • 346
  • 4
  • 9
  • 1
    No 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 http://msdn.microsoft.com/en-us/library/ms178685.aspx Your technique only looks at the lowest web.config file. – Joe Sep 18 '08 at 13:38
  • 1
    XPath 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. – Anthony Mason Mar 28 '16 at 21:18