2010-09-06 27 views
0

我試圖使用Microsoft.Web.Administration API訪問'system.webServer /安全/授權'部分(在當前請求路徑)來查看匿名用戶(「*」)是否可以訪問。檢索「system.webServer/security/authorization」配置與Microsoft.Web.Administration API

要做到這一點我試圖通過訪問該部分配置:

WebConfigurationManager.GetSection(HttpContext.Current, "system.webServer/security/authorization") 

我想,它的web.config文件被限制訪問特定的角色,這樣的路徑:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
     <security> 
      <authorization> 
       <remove users="*" roles="" verbs="" /> 
       <add accessType="Allow" roles="MyRole" /> 
      </authorization> 
     </security> 
    </system.webServer> 
</configuration> 

令我驚訝的是,返回的對象是具有零ChildElements的ConfigurationSection實例。

這是爲什麼?

在此先感謝。

回答

1

在C:\ Windows \ System32 \ inetsrv \ config \ schema \ IIS_schema.xml中查看配置架構後,我發現授權元素下的元素實際上是一個集合。

這意味着獲取集合元素我必須使用GetCollection()方法,而不是訪問ChildElements屬性:

 WebConfigurationManager.GetSection("system.webServer/security/authorization", HttpContext.Current.Request.Path).GetCollection()