检测Web.Config身份验证模式

时间:2008-09-18 11:47:15

标签: c# asp.net authentication web-config

说我有以下web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <authentication mode="Windows"></authentication>
    </system.web>
</configuration>

使用ASP.NET C#,如何检测Authentication标记的Mode值?

5 个答案:

答案 0 :(得分:28)

身份验证部分的模式属性:AuthenticationSection.Mode Property (System.Web.Configuration)。你甚至可以修改它。

// Get the current Mode property.
AuthenticationMode currentMode = 
    authenticationSection.Mode;

// Set the Mode property to Windows.
authenticationSection.Mode = 
    AuthenticationMode.Windows;

本文介绍how to get a reference to the AuthenticationSection

答案 1 :(得分:11)

导入System.Web.Configuration命名空间并执行以下操作:

var configuration = WebConfigurationManager.OpenWebConfiguration("/");
var authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication");
if (authenticationSection.Mode == AuthenticationMode.Forms)
{
  //do something
}

答案 2 :(得分:4)

尝试Context.User.Identity.AuthenticationType

去找PB的答案人

答案 3 :(得分:3)

您还可以使用静态ConfigurationManager类获取该部分,然后使用枚举AuthenticationMode来获取身份验证模式。

AuthenticationMode authMode = ((AuthenticationSection) ConfigurationManager.GetSection("system.web/authentication")).Mode;

The difference between WebConfigurationManager and ConfigurationManager

如果要在指定的枚举中检索常量的名称,可以使用Enum.GetName(Type, Object)方法

来执行此操作
Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"

答案 4 :(得分:-3)

使用xpath查询//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;
}