如何找到部门经理

时间:2013-05-20 16:36:04

标签: c# active-directory ldap

我目前正在使用Active Directory。我能够列出在部门工作的每个人的清单,但我不知道如何判断哪一个是经理。

public void MemberOf(string department)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://server.server.com");

            DirectorySearcher ds = new DirectorySearcher(de);

            ds.Filter = ("(&(objectCategory=person)(objectClass=User)(department=" + department + "))");
            ds.SearchScope = SearchScope.Subtree;

            foreach (SearchResult temp in ds.FindAll())
            {
                string test1 = temp.Path;
            }
        }

这将返回一个人员列表,其中一人是经理,其余人员直接向经理报告。

1 个答案:

答案 0 :(得分:2)

这不是最好的实现,但不知道你想用它做什么...你将如何获取数据...等等这是一个快速而肮脏的实现:

private void Test(string department)
    {
        //Create a dictionary using the manager as the key, employees for the values
        List<Employee> employees = new List<Employee>();

        DirectoryEntry de = new DirectoryEntry("LDAP://server.server.com");
        DirectorySearcher ds = new DirectorySearcher(de);

        ds.Filter = String.Format(("(&(objectCategory=person)(objectClass=User)(department={0}))"), department);
        ds.SearchScope = SearchScope.Subtree;

        foreach (SearchResult temp in ds.FindAll())
        {
            Employee e = new Employee();

            e.Manager = temp.Properties["Manager"][0].ToString();
            e.UserId = temp.Properties["sAMAccountName"][0].ToString();
            e.Name = temp.Properties["displayName"][0].ToString();

            employees.Add(e);
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public string Manager { get; set; }
        public string UserId { get; set; }
    }