【问题标题】:Detecting Web.Config Authentication Mode检测 Web.Config 认证模式
【发布时间】:2010-09-10 15:32:26
【问题描述】:

假设我有以下 web.config:

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

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

【问题讨论】:

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


    【解决方案1】:

    在 ASP.Net Core 中你可以使用这个:

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

    【讨论】:

      【解决方案2】:

      你也可以通过静态ConfigurationManager类获取section然后枚举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"
      

      【讨论】:

        【解决方案3】:

        试试Context.User.Identity.AuthenticationType

        去寻求PB的答案

        【讨论】:

        • 我接受了你的回答,因为你的回答是最快且有效的:)
        • 这是错误的。一般情况下,IIdentity.AuthenticationType 可以包含任何字符串,不一定匹配 web.config 中设置的身份验证模式。我会使用@pb 的解决方案。
        【解决方案4】:

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

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

        【讨论】:

        • 你应该使用应用程序根"~"而不是站点根"/",但最好直接调用WebConfigurationManager.GetSection("system.web/authentication")
        【解决方案5】:

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

        【讨论】:

        • 不,这在一般情况下不起作用。 ASP.NET 应用程序从 machine.Config 和虚拟目录树中更高的所有其他 web.config 文件继承设置:请参阅msdn.microsoft.com/en-us/library/ms178685.aspx 您的技术只查看最低的 web.config 文件。
        • XPath 不应该用于以任何方式解析配置。利用 MS 提供的库是一种更有效且可维护的方法。上面的评论是一个完美的例子,说明了为什么不使用它,以及并非所有平台都必须使用配置文件进行身份验证或其他设置;另一个有效的情况是,如果身份验证类型的位置发生了变化,那么您必须替换硬编码字符串,重新编译,然后重新分发。
        【解决方案6】:

        身份验证部分的模式属性: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

        【讨论】:

          猜你喜欢
          • 2012-01-26
          • 2012-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-02
          • 2011-10-20
          相关资源
          最近更新 更多