代码之家  ›  专栏  ›  技术社区  ›  Dustin Kendall

在IIS 7.0中以编程方式启用窗体身份验证

  •  1
  • Dustin Kendall  · 技术社区  · 15 年前

    我正在使用System.DirectoryServices.DirectoryEntry目录以及其中的“AuthFlags”属性来设置对虚拟web的匿名访问。要启用匿名访问,我将其值设为1。启用窗体身份验证需要设置什么值?

    3 回复  |  直到 15 年前
        1
  •  3
  •   erlando    15 年前

    我注意到你在用 System.DirectoryServices

    在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

        2
  •  2
  •   Matt Poland    15 年前

    如果要维护iis7或7.5,我建议使用稍微不同的方法。这些概念是相似的,但并不强调ASP.Net面向<系统.web>在本地应用程序中web.config文件以换取强调以IIS为导向<system.webServer>在服务器中applicationHost.config文件.

    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>
    
        3
  •  1
  •   Zakos    12 年前

    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();
          }
       }
    }
    
    推荐文章