【问题标题】:C# - Retrieve Group members of Active Directory using LDAPC# - 使用 LDAP 检索 Active Directory 的组成员
【发布时间】:2020-05-12 10:33:03
【问题描述】:

我的 Active Directory 中有一个“我的组”组,我想使用 Active Directory LDAP 从该组中检索用户。

如何修改我的查询以包含 Group 并从中获取成员?

string username = “ldapuser”;
string password = “prime812”;
DirectoryEntry de = new DirectoryEntry(“LDAP://AM-LDAP-SN.ams.com/389/CN=Users,DC=ms,DC=ds,DC=AMS,dc=com”, username,password);
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.SearchScope = SearchScope.Subtree;

forreach(SearchResult sResultSet in deSearch.FindAll())
{
}

【问题讨论】:

    标签: c# active-directory ldap


    【解决方案1】:

    我会使用 PrincipalContext 而不是:

    private static List<Principal> GetUsersOfGroup(string groupname)
    {
        string username = "ldapuser";
        string password = "prime812";
    
        using(var pc = new PrincipalContext(ContentType.Domain, null, username, password))
        {
            var _users = new List<Principal>();
            var _group = GroupPrincipal.FindByIdentity(pc, groupname);
    
            foreach(var member in group.GetMembers())
            {
                if(member is UserPrincipal _user)
                    _users.Add(_user);
            }
    
            return _users;
        }
    }
    

    【讨论】:

    • 工作,但打印每个用户需要 1 秒,我有几乎成千上万的用户.. 任何线索为什么它这么慢?
    • 主要对象可能会减慢速度,请尝试仅返回您需要的信息列表而不是对象。我会做类似return _users.Select(o =&gt; new { o.SamAccountName, o.DisplayName }).ToList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多