2009-04-24 16 views
0

IISサイトの認証を設定する必要があります。 .GetLocationPaths()を実行して、場所の文字列配列を返すことができます。しかし、私は彼らの子供の要素に入る必要があります。IIS7プログラムで場所ノードを取得

要素はsystem.applicationHost要素またはsystem.webServer要素内にネストされていないため、config.getSectionを使用して要素を抽出することはできません。

アイデア?

答えて

1

私はその後、ちょうど許可または自分のアプリケーションで拒否を変更し、私は、web.configファイル

Allow Anonymous Access 
<authorization> 
    <allow users="?" /> 
</authorization> 

Disable Anonymous Access 
<authorization> 
    <deny users="?" /> 
</authorization> 

に必要なものを置くことになりました。

Public Shared Function GetWebConfigFileName(ByVal sitePath As String) As String 
    Return IO.Path.Combine(sitePath, "web.config") 
End Function 

Private Shared Function IIS7EnableActiveDir(ByVal sitepath As String, ByVal EnableAD As Boolean) As Boolean 
    Using manager As New ServerManager 

     For Each Site In manager.Sites 

      If Not IsNothing(manager.Sites(Site.Name)) Then 
       If Not IsNothing(manager.Sites(Site.Name).Applications.Item("/")) Then 
        For Each Application In manager.Sites(Site.Name).Applications 
         If Application.VirtualDirectories.Item(0).PhysicalPath.ToString.ToLower = sitepath.ToLower Then 
          If Not String.IsNullOrEmpty(sitepath) Then 
           Dim config As Xml.XmlDocument = New Xml.XmlDocument 
           config.Load(SettingsManager.GetWebConfigFileName(sitepath)) 
           For Each node In config.SelectSingleNode("configuration/system.web/authorization") 
            If TypeOf node Is Xml.XmlElement Then 
             If EnableAD Then 
              If node.Name = "allow" Then 
               'Create new xml attribute 
               Dim newElement As Xml.XmlElement = config.CreateElement("deny") 
               Dim newElementAtt As Xml.XmlAttribute = config.CreateAttribute("users") 
               newElementAtt.Value = "?" 
               newElement.Attributes.Append(newElementAtt) 
               config.SelectSingleNode("configuration/system.web/authorization").RemoveAll() 
               config.SelectSingleNode("configuration/system.web/authorization").AppendChild(newElement) 
               config.Save(SettingsManager.GetWebConfigFileName(sitepath)) 
               Return True 
              End If 
             Else 
              If node.Name = "deny" Then 
               'Create new xml attribute 
               Dim newElement As Xml.XmlElement = config.CreateElement("allow") 
               Dim newElementAtt As Xml.XmlAttribute = config.CreateAttribute("users") 
               newElementAtt.Value = "?" 
               newElement.Attributes.Append(newElementAtt) 
               config.SelectSingleNode("configuration/system.web/authorization").RemoveAll() 
               config.SelectSingleNode("configuration/system.web/authorization").AppendChild(newElement) 
               config.Save(SettingsManager.GetWebConfigFileName(sitepath)) 
               Return True 
              End If 
             End If 
            End If 
           Next 
          End If 
         End If 
        Next 
       End If 
      End If 
     Next 
    End Using