我正在嘗試獲取域上活動目錄中所有用戶的列表。下面的代碼被使用,但似乎並沒有工作:獲取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
,但不知道爲什麼其他信息阻止它的工作...
那麼,你實際上是宣告'_ldapServerName',以及有什麼用這個變量的;它沒有用在你的代碼中!? – 2011-03-16 19:35:16
好吧,我拿出來了,現在沒有錯誤,但我沒有得到任何價值。我得到錯誤:錯誤是服務器上沒有這樣的對象。 **我如何找到我的LDAP?** – StealthRT 2011-03-16 19:52:09