2011-03-16 169 views
2

我正在嘗試獲取域上活動目錄中所有用戶的列表。下面的代碼被使用,但似乎並沒有工作:獲取Active Directory中的所有用戶

Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable 
    'To retrieve list of all LDAP users 

    'This function returns HashTable 
    _ldapServerName = ldapServerName 

    Dim sServerName As String = "mail" 

    Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _ 
      "/ou=People,dc=mydomainname,dc=com") 

    Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot) 
    Dim oResults As SearchResultCollection 
    Dim oResult As SearchResult 
    Dim RetArray As New Hashtable() 

    Try 

     oSearcher.PropertiesToLoad.Add("uid") 
     oSearcher.PropertiesToLoad.Add("givenname") 
     oSearcher.PropertiesToLoad.Add("cn") 
     oResults = oSearcher.FindAll 

     For Each oResult In oResults 

      If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then 
       RetArray.Add(oResult.GetDirectoryEntry().Properties("uid").Value, _ 
        oResult.GetDirectoryEntry().Properties("cn").Value) 
      End If 

     Next 

    Catch e As Exception 

     'MsgBox("Error is " & e.Message) 
     Return RetArray 

    End Try 

    Return RetArray 

End Function 

只是爲了確保我正確地這樣做,ldapServerName應該是我登錄到該域名我看到的時候我CTRL + alt + del,對嗎?或者會進入dc=mydomainname部分?

在上面的代碼中的第一誤差是_ldapServerName = ldapServerName

該錯誤是說是:

Error 14 '_ldapServerName' is not declared. It may be inaccessible due to its protection level. 

marc_s更新

' create a domain context for your default domain 
    Dim ctx As New PrincipalContext(ContextType.Domain) 

    ' define a "query-by-example" to search for 
    Dim searchExample As Principal = New UserPrincipal(ctx) 

    ' define the principal searcher, based on that example principal 
    Dim ps As New PrincipalSearcher(searchExample) 

    ' loop over all principals found by the searcher 
    For Each p As Principal In ps.FindAll() 
     ' do whatever you want to do with the principals 
     Console.WriteLine("Type: {0}/Name: {1}", p.StructuralObjectClass, p.Name) 
    Next 

更新2

當我使用IE瀏覽器和輸入ldap://mydomainhere.com/ou=Users

我沒有得到任何東西......但是,當我只是這樣做:

ldap://mydomainhere.com 

然後我得到了「找人」框彈出。所以我知道我有正確的LDAP,但不知道爲什麼其他信息阻止它的工作...

+0

那麼,你實際上是宣告'_ldapServerName',以及有什麼用這個變量的;它沒有用在你的代碼中!? – 2011-03-16 19:35:16

+0

好吧,我拿出來了,現在沒有錯誤,但我沒有得到任何價值。我得到錯誤:錯誤是服務器上沒有這樣的對象。 **我如何找到我的LDAP?** – StealthRT 2011-03-16 19:52:09

回答

2

如果你的廣告不是太大,你在.NET 3.5或以上(我假設,因爲你正在使用VS2010),你應該能夠寫類似:

// create a domain context for your default domain 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" to search for 
Principal searchExample = new UserPrincipal(ctx); 

// define the principal searcher, based on that example principal 
PrincipalSearcher ps = new PrincipalSearcher(searchExample); 

// loop over all principals found by the searcher 
foreach(Principal p in ps.FindAll()) 
{ 
    // do whatever you want to do with the principals 
    Console.WriteLine("Type: {0}/Name: {1}", p.StructuralObjectClass, p.Name); 
} 

PS:爲了「找到你的LDAP」,你可以看看我的C#,開源名爲BeaverTail的LDAP瀏覽器 - 免費提供(C#,.NET 1.1時間範圍)

enter image description here

更新:,如果你想選擇在特定位置的所有用戶(及其子容器),您可以通過指定做到這一點,在你的域範圍內的「起點」:

// create a domain context for your default domain, 
// starting at a specific location 
PrincipalContext ctx = 
    new PrincipalContext(ContextType.Domain, "YOURDOMAIN", 
         "OU=Personnel,OU=Users,DC=YourDomain,DC=com"); 

// define a "query-by-example" to search for 
Principal searchExample = new UserPrincipal(ctx); 

// define the principal searcher, based on that example principal 
PrincipalSearcher ps = new PrincipalSearcher(searchExample); 

// loop over all principals found by the searcher 
foreach(Principal p in ps.FindAll()) 
{ 
    UserPrincipal up = (p as UserPrincipal); 

    if(up != null) 
    { 
     // do whatever you want to do with the principals 
     Console.WriteLine("Name: {0}/E-Mail: {1}", up.Name, up.EmailAddress); 
    } 
} 
+0

當它轉換爲VB時,似乎無法讓您的代碼正常工作。檢查OP。不知道PrincipalContext,UserPrincipal,PrincipalSearcher是什麼。 – StealthRT 2011-03-16 19:51:49

+0

@StealthRT:你需要添加一個對System.DirectoryServices.AccountManagement程序集(和命名空間)的引用 - 對不起,我的壞... – 2011-03-16 19:57:44

+0

確定添加了,但運行時出現這個錯誤:**指定的目錄服務屬性或值不存在。**在以下行上:** Dim ps As New PrincipalSearcher(searchExample)**。對此有何想法? – StealthRT 2011-03-16 20:06:42

0

在我的特定情況下,我不得不使用:

Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://CN=Users,DC=YOUR_DOMAIN_NAME_HERE,DC=local") 
+0

所以你不必爲LDAP:// {這裏}放任何東西? – StealthRT 2011-03-17 13:50:06

+0

如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)以精確地格式化和語法突出顯示它! – 2011-03-17 16:47:55

1
CStr(userlist(i).Properties("samAccountName").ToString) 

更改爲:

userlist(i).GetDirectoryEntry().Properties("givenName").Value 

這一變化爲我工作

相關問題