简体   繁体   English

LDAP 连接到 AD 并搜索所有 OU 中的所有用户

[英]LDAP Connection to AD and search on all User in all OUs

Im using the Code from: How can I get a list of users from active directory?我使用的代码来自: 如何从活动目录中获取用户列表? to get all User from my AD.从我的广告中获取所有用户。

Now im trying to connect via LDAP to a Domain to get all Users from that Active Directory with the following changes:现在,我尝试通过 LDAP 连接到域,以通过以下更改从该 Active Directory 中获取所有用户:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "192.168.1.100", "Username@testdomain.local", "Password"))

There are 2 different OUs at testdomain.local with Users but Im only getting the Users of one OU? testdomain.local 上有 2 个不同的 OU 和用户,但我只获得了一个 OU 的用户? I thought that gives me all Users from all OUs from AD?我认为这给了我来自 AD 的所有 OU 的所有用户?

If I use the following for my current AD Domain then I get all USers from all OUs?如果我对当前的 AD 域使用以下内容,那么我会从所有 OU 中获取所有用户吗?

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, currentDomain))

Could that be a configuration problem on the other domain or is the Code not working with a LDAP Connection?这可能是另一个域上的配置问题,还是代码无法与 LDAP 连接一起使用?

Thank you谢谢

UPDATE:更新:

Code:代码:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "192.168.1.100", "Username@testdomain.local", "Password"))
{
    using (PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            de.Properties["samAccountName"].Value
        }
        catch (Exception c)
        {
        }
        result.Dispose();
    }
}

The code you have above works fine and pull all the records without any errors or skips.您上面的代码运行良好,可以提取所有记录而没有任何错误或跳过。 I would recommend changing the domain name from IP address to testdomain.local and username without @testdomain.local.我建议将域名从 IP 地址更改为testdomain.local和不使用 @testdomain.local 的用户名。

If you are looking to get samAccountName, or other properties, and convert them to DirectoryEntry, I would recommend the following route that uses DirectorySearcher.如果您想获取 samAccountName 或其他属性,并将它们转换为 DirectoryEntry,我会推荐以下使用 DirectorySearcher 的路线。 It provides better performance and looks up in all OUs.它提供更好的性能并在所有 OU 中查找。

DirectoryEntry entry = new DirectoryEntry("LDAP://DC=testdomain,DC=local", "username", "password");
string searchQuery = $"(&(objectCategory=user)(objectClass=user))";

var listOfUsers = new List<string>();
DirectorySearcher ds = new DirectorySearcher(entry, searchQuery,
                            new string[] { "samAccountName" });
ds.SizeLimit = int.MaxValue;
ds.PageSize = int.MaxValue;
foreach (SearchResult user in ds.FindAll())
{
    string samAccountName = user.Properties["samAccountName"][0].ToString();
    Console.WriteLine(samAccountName);
    listOfUsers.Add(samAccountName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM