【问题标题】:.NET Active Directory - Get a list of users in a specific Active Directory Group.NET Active Directory - 获取特定 Active Directory 组中的用户列表
【发布时间】:2016-01-04 23:29:28
【问题描述】:

我有一个使用 Ldap 查询的组名称列表...我已将列表名称绑定到 WinForms 应用程序中的数据网格。当用户选择其中一个组名时,会触发一个事件并将组名传递给以下方法:-

    // Get a list of group specific users //
    private List<Users> GetUsers(string groupName)
    {
        List<Users> groupSpecificUsers = new List<Users>();
        DirectorySearcher ds = null;
        DirectoryEntry de = new DirectoryEntry(domainPath);
        ds = new DirectorySearcher(de);

        ds.PropertiesToLoad.Add("SAMAccountName");
        ds.PropertiesToLoad.Add("member");
        ds.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";
        SearchResult sr = ds.FindOne();

        if (sr != null)
        {
                // do whatever you need to do with the entry
        }

.... return list of users that belong to the specific GroupName ....

当我在 if 语句处设置断点时... sr 被列为 null...我不明白为什么它为 null...即使所选组中显然有成员...

我觉得,我不太明白如何在 ldap 查询中使用特定的组名......谁能指出我正确的方向?

【问题讨论】:

  • 我认为 sr 可能为空,因为您在 SAMAccountName 上进行搜索,但您提供的是组名而不是帐户名。 SAMAccountName 应该是用于登录 AD 的名称。因此 ds.filter 行只会返回 1 个项目(假设某人的帐户名与组名相同)

标签: .net winforms active-directory ldap


【解决方案1】:

您的 DirectoryEntry 对象采用 domainPath 的参数,我认为它是您代码中某处的一个字段(?)。如果你可以尝试从根目录搜索,你可以试试这个代码,看看你是否得到了更好的结果:

// Get a list of group specific users //
private List<Users> GetUsers(string groupName)
{
    List<Users> groupSpecificUsers = new List<Users>();
// MAKE SURE THE NEXT LINE REFLECTS YOUR DOMAIN
    DirectorySearcher ds = (new DirectoryEntry("LDAP://dc=yourdomain,dc=tld"));
    ds.PropertiesToLoad.Add("samaccountname");
    ds.PropertiesToLoad.Add("member");
    ds.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";
    SearchResult sr = ds.FindOne();

    if (sr != null)
    {
            // do whatever you need to do with the entry
    }

查看这些更改是否能解决您的问题。

【讨论】:

  • 感谢 Sam,我收到错误消息...无法从目录条目隐式转换为目录搜索器。在 LDAP://dc=yourdomain 语句中。
  • 我将其更改为 Directory Searcher 并修复了语法错误。但是由于 sr.... 的结果仍然为 null。
  • 对语法错误表示歉意。我也测试了它,但正在得到结果。您确定 DirectoryEntry 的 LDAP:// 参数是正确的,并且该组实际上是成员吗?
  • 是的。好的,我解决了。感谢您提供根目录的想法。
  • 嗨,Sam,我还有一个问题,如果你愿意看,请告诉我?
【解决方案2】:

我认为下面一行:

ds.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";

需要改成:

ds.Filter = "(&(objectClass=group)(Group=" + groupName + "))";

【讨论】:

  • 您可能需要添加“ds.PropertiesToLoad.Add("Group");"在运行过滤器之前。
  • 还是不行。 //ds.PropertiesToLoad.Add("SAMAccountName"); //ds.PropertiesToLoad.Add("member"); ds.PropertiesToLoad.Add("组"); ds.Filter = "(&(objectClass=group)(Group=" + groupName + "))";
【解决方案3】:

这就是我解决它的方法(这几乎是 Sam 所说的,为了便于说明,我对其进行了更多调整):-

            List<Users> groupSpecificUsers = new List<Users>();
            DirectoryEntry ROOT = new DirectoryEntry("LDAP://DC=xxx,DC=net");
            DirectoryEntry de = ROOT;

            var sr = new DirectorySearcher(de);
            sr.PropertiesToLoad.Add("SAMAccountName");
            sr.PropertiesToLoad.Add("member");
            sr.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";



            if (sr != null)
            {...whatever...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多