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

Iis 7 在IIS 7.0中以编程方式启用窗体身份验证,iis-7,c#-3.0,forms-authentication,Iis 7,C# 3.0,Forms Authentication,我目前正在使用System.DirectoryServices.DirectoryEntry和其中的“AuthFlags”属性设置对虚拟web的匿名访问。要启用匿名访问,我将其值设为1。我需要设置什么值才能启用forms auth 我脑子里有一个想法,可能这只是通过web.config设置的?我注意到您正在使用System.DirectoryServices根据您的标记在IIS7上配置这些功能 在IIS7中,您可以使用Microsoft.Web.Administration库来配置这两种设置:

我目前正在使用System.DirectoryServices.DirectoryEntry和其中的“AuthFlags”属性设置对虚拟web的匿名访问。要启用匿名访问,我将其值设为1。我需要设置什么值才能启用forms auth

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

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

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

设置身份验证类型将替换AuthFlags:

要配置表单身份验证,请执行以下操作:

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的引用。

我注意到您正在使用System.DirectoryServices根据标记在IIS7上配置这些功能

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

设置身份验证类型将替换AuthFlags:

要配置表单身份验证,请执行以下操作:

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的引用。

如果要维护IIS 7或7.5,我建议使用稍微不同的方法。这些概念类似,但在本地应用程序web.config中不再强调面向ASP.Net的内容,而是在服务器应用程序host.config中强调面向IIS的内容

从链接底部开始,向上滚动。。。

核心方法是在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>

如果要维护IIS 7或7.5,我建议使用稍微不同的方法。这些概念类似,但在本地应用程序web.config中不再强调面向ASP.Net的内容,而是在服务器应用程序host.config中强调面向IIS的内容

从链接底部开始,向上滚动。。。

核心方法是在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>

就像我在上一句中说的,你是说最好的方法就是修改web.config?谢谢你这么说。@zip-是的,这就是解决问题的方法。就像我在上一句话中说的,你是说最好的方法就是修改web.config?谢谢你的提示。@zip-是的,这就是解决问题的方法。我不明白的是/App1从哪里来?我不明白的是/App1从哪里来?