【问题标题】:Get all users in the Active Directory获取 Active Directory 中的所有用户
【发布时间】: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.

ma​​rc_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


【解决方案1】:

如果您的 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# 开源 LDAP 浏览器 BeaverTail - 免费提供(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);
    }
}

【讨论】:

  • 将代码转换为 VB 时似乎无法正常工作。检查 OP。不知道 PrincipalContext、UserPrincipal、PrincipalSearcher 是什么。
  • @StealthRT:您需要添加对System.DirectoryServices.AccountManagement 程序集(和命名空间)的引用 - 抱歉,我的错......
  • 好的,但运行时出现此错误:指定的目录服务属性或值不存在。 在以下行:Dim ps As New PrincipalSearcher (搜索示例)。对此有什么想法吗?
  • @StealthRT:没有工作对我来说很好......嗯......你在那个域的成员上运行代码的机器是你试图找到用户的吗??
  • @marc_d:是的,它在同一个域上。当我想共享一个驱动器号时,我可以调出 AD,它提供了 300 多个用户,所以我知道我在域中。
【解决方案2】:
CStr(userlist(i).Properties("samAccountName").ToString)

改为:

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

这种改变对我有用

【讨论】:

    【解决方案3】:

    在我的特定情况下,我不得不使用:

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

    【讨论】:

    • 所以您不必为 LDAP://{here} 输入任何内容?
    • 如果您发布代码、XML 或数据示例,请在文本编辑器中突出显示这些行,然后单击编辑器上的“代码示例”按钮 ({ })工具栏以很好地格式化和语法突出显示它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多