以编程方式在IIS 7.0中启用表单身份验证

时间:2010-09-27 15:47:14

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

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

我有这个想法,可能这只是通过web.config设置的?

3 个答案:

答案 0 :(得分:3)

我注意到您正在使用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的引用。

答案 1 :(得分:2)

如果维护IIS 7或7.5,我建议采用略有不同的方法。这些概念类似,但不再强调面向ASP.Net的&lt; system.web&gt;在交易中的本地应用程序web.config中强调面向IIS的&lt; system.webServer&gt;在服务器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>

答案 2 :(得分:1)

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();
      }
   }
}