2

我設法做ASP.NET驗證工作王氏AD。現在,我想查詢AD中的OU,並在ASP.NET頁面中顯示結果 ListView或GridView。查詢Active Directory以列表視圖

這裏的域控制器:dc.itlab.edu

的OU:UsersStudents

在組織單位(OU)UsersStudents有以下欄目:

名,姓,Windows 2000以前版本的登錄名,名稱,類型

我想查詢列名,姓,Windows 2000以前登錄在OU UsersStudents中命名,並將 結果綁定到ListView或GridView。

感謝您無論是在C#或VB.NET建議。

回答

4

如果你在.NET 3.5,或者可以升級到它 - LDAP的東西已經大大提高引進的System.DirectoryServices.AccountManagement命名空間。

它包含了其他的東西類,如UserPrincipal,它提供了最常用的LDAP屬性的屬性之一。通過使用PrincipalSearcher和QBE(按示例查詢),您可以輕鬆找到您感興趣的用戶(或其他對象)並將它們綁定到ASP.NET網格視圖。

要了解更多有關新的.NET 3.5的東西,閱讀在MSDN雜誌這個優秀的文章:

Managing Directory Security Principals in the .NET Framework 3.5 - January 2008 issue

更新:使用.NET 3.5的界面,您可以編寫代碼是這樣的:

// define the content - domain name (second param) must be NetBIOS-style, 
// third parameter is the container where to create the context for 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ITLAB", "OU=UsersStudents,DC=dc,DC=itlab,DC=edu"); 

// define your "prototype" for the searcher - here: you want to search for 
// users which have the .Enabled property set to true; you could define additional 
// requirements here 
UserPrincipal qbePrototype = new UserPrincipal(ctx); 
qbePrototype.Enabled = true; 

// create PrincipalSearcher based on that QBE prototype 
PrincipalSearcher ps = new PrincipalSearcher(qbePrototype); 

// find all matching Principals - in your case, those will be of type UserPrincipal 
PrincipalSearchResult<Principal> results = ps.FindAll(); 

現在你應該可以在results直接綁定到一個DataGridView什麼的,並挑選出那些屬性爲您的山坳

  • 名= UserPrincipal.GivenName
  • 姓= UserPrincipal.Surname
  • Windows 2000以前版本的登錄名= UserPrincipal.SamAccountName
  • 名稱=名稱
  • :那你要找的UMNS
  • Type = ??你在這裏意味着什麼?
+0

是的,我正在使用.NET 3.5 – Narazana 2010-10-25 15:56:08

+0

OU中有一列「Type」。類型 - >用戶 – Narazana 2010-10-25 16:43:07

+0

如果你只是在搜索用戶,那麼這個類型將永遠是用戶無論如何..... – 2010-10-25 16:48:46

0

未測試**這將指向你在正確的方向..應該很接近你所需要的。

Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("LDAP://domain/DC=..", "usr", "pwd") 
    Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot) 

    MyDirectorySearcher.Filter = ("(&(objectCategory=organizationalunit)(name=UsersStudents))") 

    MyDirectorySearcher.SearchScope = SearchScope.Subtree 
    MyDirectorySearcher.PropertiesToLoad.Add("First Name") 
    MyDirectorySearcher.PropertiesToLoad.Add("Last Name") 
    MyDirectorySearcher.PropertiesToLoad.Add("Pre-Windows 2000 Logon Name") 
    MyDirectorySearcher.PropertiesToLoad.Add("Name") 
    MyDirectorySearcher.PropertiesToLoad.Add("Type") 
    MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending 
    MyDirectorySearcher.Sort.PropertyName = "Name" 

    Dim MySearchResult As SearchResultCollection = MyDirectorySearcher.FindAll() 

    Dim myTable As New DataTable("Results") 
    Dim colName As String 

    For Each colName In MyDirectorySearcher.PropertiesToLoad 
     myTable.Columns.Add(colName, GetType(System.String)) 
    Next 

    Dim result As SearchResult 

    For Each result In MySearchResult 
     Dim dr As DataRow = myTable.NewRow() 
     For Each colName In MyDirectorySearcher.PropertiesToLoad 
      If result.Properties.Contains(colName) Then 
        dr(colName) = CStr(result.Properties(colName)(0)) 
       End If 
      Else 
       dr(colName) = "" 
      End If 
     Next 
     myTable.Rows.Add(dr) 
    Next 

    gridview.datasource = myTable 
    gridview.databind() 
相關問題