我正在尝试获取域中活动目录中所有用户的列表。正在使用以下代码,但似乎不起作用:
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 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
更新2
当我使用IE并输入ldap://mydomainhere.com/ou=Users
我什么都没得到......但是当我这样做时:
ldap://mydomainhere.com
然后我弹出“找人”框。所以我知道我有正确的LDAP
,但不确定为什么其他信息阻止它工作......
答案 0 :(得分:2)
如果您的AD不是太大,并且您使用的是.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时间表)
更新:如果要选择特定位置(及其子容器)中的所有用户,可以通过在域上下文中指定“起点”来执行此操作:
// 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);
}
}
答案 1 :(得分:1)
CStr(userlist(i).Properties("samAccountName").ToString)
更改为:
userlist(i).GetDirectoryEntry().Properties("givenName").Value
这种改变对我有用
答案 2 :(得分:0)
在我的特定情况下,我不得不使用:
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://CN=Users,DC=YOUR_DOMAIN_NAME_HERE,DC=local")