2010-09-27 41 views
1

我目前使用System.DirectoryServices.DirectoryEntry和「AuthFlags」屬性在其中設置匿名訪問虛擬Web表單的認證。要啓用匿名訪問,我給它一個值1.我需要設置什麼值來啓用表單身份驗證?編程啓用IIS 7.0

我有這樣的想法在我的後腦勺,也許這通過在web.config中只設置?

回答

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

+0

所以有點像我剛纔說的我最後一句,你說最好的方法是修改web.config?感謝這個順便說一句。 – 2010-09-29 07:42:36

+0

@zip - 是的,這將是做到這一點的方式。 – Kev 2010-09-29 07:56:58

2

如果維護IIS 7或7.5,我會推薦一個稍微不同的方法。概念相似,但去強調ASP.Net面向<的System.Web >在本地應用程序的web.config中貿易的強調服務器的applicationHost.config面向<system.webServer>的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 \設置\的applicationHost.config

事情來尋找:

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

我不明白的是/ App1來自哪裏? – 2012-12-12 05:38:08

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