加载活动目录用户及其关联的计算机

本文关键字:关联 计算机 用户 活动 加载 | 更新日期: 2023-09-27 18:10:48

大家好,我正在努力开发一个程序,将列出活动目录的所有用户,当我选择一个用户时,该程序应该能够显示与该用户关联的计算机。即该AD用户可以访问的计算机。我已经编写了代码来列出所有用户,但不知道如何列出与该用户相关的计算机。下面是我的代码加载AD用户到datatable:

DataTable dtUser= new DataTable();
    try
    {            
        DirectoryEntry dom = Domain.GetComputerDomain().GetDirectoryEntry();
        DirectorySearcher dsAllUsers = new DirectorySearcher(dom);
        dsAllUsers.SearchScope = SearchScope.Subtree;
        dsAllUsers.Filter = "(objectCategory=Person)";
        SearchResultCollection result = dsAllUsers.FindAll();
        dtUser.Columns.Add("CustodianName");
        dtUser.Columns.Add("Email");
        dtUser.Columns.Add("Title");
        dtUser.Columns.Add("Dept.");           
        foreach (SearchResult rs in result)
        {
            DataRow newRow = dtUser.NewRow();
            if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
                newRow["CustodianName"] = rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();
            if (rs.GetDirectoryEntry().Properties["mail"].Value != null)
                newRow["Email"] = rs.GetDirectoryEntry().Properties["mail"].Value.ToString();
            if (rs.GetDirectoryEntry().Properties["title"].Value != null)
                    newRow["Title"] = rs.GetDirectoryEntry().Properties["title"].Value.ToString();
            if (rs.GetDirectoryEntry().Properties["department"].Value != null)
                newRow["Dept."] = rs.GetDirectoryEntry().Properties["department"].Value.ToString();
            dtUser.Rows.Add(newRow);
        }
        return dtUser;
    }
    catch (Exception)
    {
        throw;
    }

加载活动目录用户及其关联的计算机

我不相信标准的LDAP/Active Directory有这样的东西。

计算机只是另一类AD对象-但是在用户和一台(或多台)计算机之间没有"链接"-在computer类上没有belongsTo属性,在User上也没有computers集合。

如果您的组织已经实现了默认AD模式的扩展(这是完全可能的!),它是一个自定义解决方案,然后必须知道它是什么!: -)