Я пытаюсь получить список всех пользователей в активном каталоге в домене.Следующий код используется, но, похоже, не работает:
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
должно быть доменным именем, в которое я вхожу, и которое я вижу, когдаI CTRL + alt + del , верно?Или это относится к dc=mydomainname
части?
Первая ошибка в этом коде выше на _ldapServerName = ldapServerName
Ошибка говорит:
Error 14 '_ldapServerName' is not declared. It may be inaccessible due to its protection level.
marc_s update
' 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
update 2
Когда я использую IE и ввод ldap://mydomainhere.com/ou=Users
Я не получаючто угодно ... Но когда я просто делаю это:
ldap://mydomainhere.com
Затем появляется окно "найти людей".Итак, я знаю, что у меня правильный LDAP
, но не уверен, почему другая информация мешает ему работать ...