GateKiller
GateKiller

Reputation: 75869

Detecting Web.Config Authentication Mode

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?

Upvotes: 15

Views: 24011

Answers (6)

SZL
SZL

Reputation: 855

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();
}

Upvotes: 0

clD
clD

Reputation: 2611

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"

Upvotes: 5

redsquare
redsquare

Reputation: 78667

Try Context.User.Identity.AuthenticationType

Go for PB's answer folks

Upvotes: 4

bkaid
bkaid

Reputation: 52073

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
}

Upvotes: 12

timvw
timvw

Reputation: 346

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;
}

Upvotes: -3

Paul van Brenk
Paul van Brenk

Reputation: 7549

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.

Upvotes: 29

Related Questions