无法为广告用户构建过滤器

时间:2019-07-30 11:58:50

标签: c# search active-directory

我正在寻找动态更改广告的事件。每个人都使用一个示例AFAICC。仅包含用户的单独OU是可以的,但是当我为整个域设置了OU时,就会发生太多事件,因此我需要过滤用户唯一的更改。目前,我有一个(objectClass=*)方法的SearchRequest过滤器,它工作正常。但是,当我将其更改为(&(objectCategory=person)(objectClass=user))(sAMAccountType=805306368)或其他任何内容时,我有了运行时DirectoryOperationalException。为什么?

我尝试更改语法,之后将已经在网上找到的一些示例直接复制粘贴到我的代码中,但是可惜。

string searchFilter = "(sAMAccountType=805306368)";

SearchRequest request = new SearchRequest(
    dn, //root the search here
    searchFilter, //"(objectClass=*)", // very inclusive
    scope, //any scope works
    null //we are interested in all attributes
);

1 个答案:

答案 0 :(得分:0)

我正在使用此..这段代码按名字,中间名缩写,姓氏查找人员详细信息。不知道您是否在寻找这个。

https://lastloop.blogspot.com/2019/07/list-of-all-active-directory-properties.html

public List<Person> FindPersonDetails(Person objPerson)
        {
            string lname = string.Empty, fname = string.Empty, mname = string.Empty, MSID = string.Empty;
            fname = objPerson.FirstName;
            lname = objPerson.LastName;
            mname = objPerson.MiddleName;
            MSID = objPerson.MSID;
            DataTable dt = new DataTable();
            dt.Columns.Add("DisplayName", typeof(string));
            dt.Columns.Add("GivenName", typeof(string));
            dt.Columns.Add("SurName", typeof(string));
            dt.Columns.Add("MSID", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("employeeid", typeof(string));

            //DirectoryEntry _objDirectoryEntry;
            DirectorySearcher searcher = new DirectorySearcher();
            if (lname != null && lname != "")
            {
                if (mname != null && mname != "")
                    searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0}*)(DisplayName={1}*)(middlename={2}*))", fname, lname, mname);
                else
                    searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0}*)(DisplayName={1}*))", fname, lname);
            }
            else if (MSID != null && MSID != "")
            {
                searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(cn={0}))", MSID);
            }
            else
            {
                if (mname != null && mname != "")
                    searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0}*)(middlename={1}*))", fname, mname);
                else
                    searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0}*))", fname);
            }
            SearchResultCollection allResults;
            //searcher.SizeLimit = 100;
            searcher.Asynchronous = true;
            //searcher.ClientTimeout = TimeSpan.FromSeconds(7);
            allResults = searcher.FindAll();
            try
            {
                if (allResults.Count >= 0)
                {
                    for (int i = 0; i < allResults.Count; i++)
                    {
                        DirectoryEntry deMembershipUser = allResults[i].GetDirectoryEntry();
                        deMembershipUser.RefreshCache();
                        dt.Rows.Add(
                            (string)deMembershipUser.Properties["displayname"].Value ?? "Not Available",
                            (string)deMembershipUser.Properties["givenName"].Value ?? "Not Available",
                            (string)deMembershipUser.Properties["sn"].Value ?? "Not Available",
                            (string)deMembershipUser.Properties["cn"].Value ?? "Not Available",
                            (string)deMembershipUser.Properties["mail"].Value ?? "Not Available",
                            (string)deMembershipUser.Properties["employeeid"].Value ?? "Not Available"
                        );
                    }
                }
            }
            catch ()
            {

            }
            dt.DefaultView.Sort = "DisplayName ASC";
            dt = dt.DefaultView.ToTable();
            List<Person> objPersonList = new List<Person>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Person obj = new Person();

                    obj.DisplayName = dr["displayname"].ToString();
                    //obj.GivenName = dr["givenName"].ToString();
                    //obj.SurName = dr["SurName"].ToString();
                    obj.Email = dr["Email"].ToString();
                    obj.MSID = dr["MSID"].ToString();
                    //obj.employeeid = dr["employeeid"].ToString();
                    objPersonList.Add(obj);
                }
            }

            return objPersonList;
        }

  public class Person
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string MSID { get; set; }
        public string DisplayName { get; set; } 
        public string Email { get; set; }
        //public string employeeid { get; set; } 
    }