views:

2275

answers:

3

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?

+1  A: 

try Context.User.Identity.AuthenticationType

redsquare
I have accepted your answer because yours was the quickest and worked :)
GateKiller
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
+8  A: 

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.

pb
A: 

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;
}
timvw
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.aspxYour technique only looks at the lowest web.config file.
Joe