假设我有以下 web.config:

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

使用 ASP.NET C#,如何检测身份验证标记的模式值?

有帮助吗?

解决方案

尝试 Context.User.Identity.AuthenticationType

去找PB的答案吧

其他提示

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

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

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

本文描述了 如何获取对 AuthenticationSection 的引用.

导入 System.Web.Configuration 命名空间并执行类似的操作:

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

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

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

WebConfigurationManager 和 ConfigurationManager 的区别


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

Enum.GetName(typeof(AuthenticationMode), authMode); // e.g. "Windows"

使用 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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top