Vb.net 获取Active Directory中的所有用户

Vb.net 获取Active Directory中的所有用户,vb.net,visual-studio-2010,active-directory,Vb.net,Visual Studio 2010,Active Directory,我正在尝试获取域上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

我正在尝试获取域上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
部分

上面代码中的第一个错误出现在
\u ldapServerName=ldapServerName

错误是:

 Error 14 '_ldapServerName' is not declared. It may be inaccessible due to its protection level.
马克的更新

    ' 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
,但不确定为什么其他信息会阻止它工作…

如果您的广告不是太大,并且您在.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”,您可以查看my C#,一个名为-免费提供的开源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);
    }
}

在我的特殊情况下,我必须使用:

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

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

这一变化对我起到了作用

那么你实际上在哪里声明
\u ldapServerName
,这个变量的用途是什么;您的代码中没有使用它!?好的,我把它拿出来了,现在没有错误,但是我没有得到任何值。我得到一个错误:错误是服务器上没有这样的对象。如何找到我的LDAP?在将其转换为VB时,您的代码似乎无法正常工作。检查OP.Doesn不知道PrincipalContext、UserPrincipal、PrincipalSearcher是什么。@StealthRT:您需要添加对
System.DirectoryServices.AccountManagement
程序集(和命名空间)的引用-抱歉,我的错误…添加了确定,但在运行它时出现此错误:指定的目录服务属性或值不存在。在线:Dim ps作为新的PrincipalSearcher(搜索示例)。有什么想法吗?@StealthRT:没有,对我来说很好。。。。隐马尔可夫模型。。。是不是你在这个域的成员上运行代码的那台机器,你在试图找到它的用户???@marc_d:是的,它在同一个域上。当我想共享一个驱动器号时,我可以打开这个广告,它有300多个用户,所以我知道我在这个域上。所以你不必为LDAP添加任何内容://{here}?如果你发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,然后单击“代码示例”按钮(
{}
)在编辑器工具栏上很好地格式化和语法突出显示它!
userlist(i).GetDirectoryEntry().Properties("givenName").Value