【发布时间】:2011-03-16 19:32:43
【问题描述】:
我正在尝试获取域上活动目录中所有用户的列表。以下代码正在使用,但似乎不起作用:
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,这个变量有什么用;它没有在你的代码中使用!? -
好的,我把它拿出来了,现在没有错误,但我没有得到任何价值。我得到错误:错误是服务器上没有这样的对象。 如何找到我的 LDAP?
标签: vb.net visual-studio-2010 active-directory