本文介绍了加载活动目录用户及其关联的计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone我正在尝试开发一个程序,它将列出所有Active Directory用户,当我选择一个用户时,程序应该能够显示与该用户关联的计算机。即该AD用户可访问的计算机。我编写了代码列出所有用户,但不知道如何列出与该用户关联的计算机。以下是将AD用户加载到数据表中的代码:



Hello Everyone i am trying to develop a program that will list will all the user of Active Directory and when i select one user the program should be able to show the computers associated with that user. i.e the Computers that are accessible to that AD user. I have written code to list all user but no idea how to list the computers associated with that user. Here is my code to load AD User into 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;
    }

推荐答案

这篇关于加载活动目录用户及其关联的计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:17