【问题标题】:Programmatically enable forms authentication in IIS 7.0以编程方式在 IIS 7.0 中启用表单身份验证
【发布时间】:2026-02-18 10:50:01
【问题描述】:

我目前正在使用 System.DirectoryServices.DirectoryEntry 和其中的“AuthFlags”属性来设置对虚拟网络的匿名访问。为了启用匿名访问,我给它一个值 1。我需要设置什么值来启用表单身份验证?

我有一个想法,也许这只能通过 web.config 设置?

【问题讨论】:

    标签: iis-7 c#-3.0 forms-authentication


    【解决方案1】:

    我注意到您正在使用System.DirectoryServices 在 IIS7 上配置这些功能(根据您的标签)。

    在 IIS7 中,您可以使用 Microsoft.Web.Administration 库来配置这两个设置:

    设置认证类型(替换AuthFlags):

    IIS 7 Configuration: Security Authentication <authentication>

    配置表单身份验证:

    using Microsoft.Web.Administration;
       ...
    long iisNumber = 1234;
    using(ServerManager serverManager = new ServerManager())
    {
      Site site = serverManager.Sites.Where(s => s.Id == iisNumber).Single();
    
      Configuration config = serverManager.GetWebConfiguration(site.Name);
      ConfigurationSection authenticationSection = 
                   config.GetSection("system.web/authentication");
      authenticationSection.SetAttributeValue("mode", "Forms");
    
      ConfigurationSection authorizationSection = 
                   config.GetSection("system.web/authorization");
      ConfigurationElementCollection addOrDenyCollection = 
                   authorizationSection.GetCollection();
      ConfigurationElement allowElement = addOrDenyCollection.CreateElement("allow");
      allowElement["users"] = "?";
    
      addOrDenyCollection.Add(allowElement);
      serverManager.CommitChanges();
    }
    

    以上代码将在网站根目录下创建一个新的web.config 文件或修改现有文件。

    要使用Microsoft.Web.Administration,请添加对C:\Windows\System32\InetSrv\Microsoft.Web.Administration.dll 的引用。

    【讨论】:

    • 就像我在上一句中所说的那样,您是说最好的方法是修改 web.config?感谢这个顺便说一句。
    • @zip - 是的,这就是这样做的方式。
    【解决方案2】:

    如果维护 IIS 7 或 7.5,我会推荐一种稍微不同的方法。概念相似,但不强调本地应用程序 web.config 中面向 ASP.Net 的 ,而是强调服务器 applicationHost.config 中面向 IIS 的

    从该链接的底部开始并向上滚动... http://www.iis.net/ConfigReference/system.webServer/security/authentication/windowsAuthentication

    Imports System
    Imports System.Text
    Imports Microsoft.Web.Administration
    
    Module Sample
       Sub Main()
          Dim serverManager As ServerManager = New ServerManager
          Dim config As Configuration = serverManager.GetApplicationHostConfiguration
    
          Dim anonymousAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Default Web Site/App1")
          anonymousAuthenticationSection("enabled") = False
    
          Dim windowsAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Default Web Site/App1")
          windowsAuthenticationSection("enabled") = True
    
          serverManager.CommitChanges()
       End Sub
    End Module
    

    核心方法是在 IIS 管理器中进行更改,并观察该应用程序的应用程序主机配置如何更改。然后通过适当地驱动新的 Microsoft.Web.Administration 程序集来复制这些更改。

    位置:%systemroot%\system32\inetsrv\config\applicationHost.config

    要寻找的东西:

    <location path="Default Web Site/App1">
        <system.webServer>
            <security>
                <authentication>
                    <anonymousAuthentication enabled="true" />
                    <windowsAuthentication enabled="true" />
                </authentication>
            </security>
        </system.webServer>
    </location>
    

    【讨论】:

    • 我不明白的是 /App1 来自哪里?
    【解决方案3】:

    Source

    using System;
    using System.Text;
    using Microsoft.Web.Administration;
    
    internal static class Sample {
    
       private static void Main() {
    
          using(ServerManager serverManager = new ServerManager()) { 
             Configuration config = serverManager.GetApplicationHostConfiguration();
    
             ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso");
             anonymousAuthenticationSection["enabled"] = false;
    
             ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso");
             windowsAuthenticationSection["enabled"] = true;
    
             serverManager.CommitChanges();
          }
       }
    }
    

    【讨论】:

      最近更新 更多