獲取Active Directory裏user的明細

 public static string[] GetDetailForUsers(string loginname)
        {
            var res = new List<string>();
            string filter = "(&(objectCategory=user)(cn="+loginname+"))";
            var searcher = new DirectorySearcher { SearchRoot = getRoot(), Filter = filter };
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.Add("givenName");
            searcher.PropertiesToLoad.Add("sn");
            searcher.PageSize = 500;
            try
            {
                using (SearchResultCollection results = searcher.FindAll())
                {
                    foreach (SearchResult result in results)
                    {
                        int resultCount = result.Properties["givenName"].Count;
                        for (int c = 0; c < resultCount; c++)
                        {
                            res.Add(result.Properties["givenName"][c].ToString());
                            res.Add(result.Properties["sn"][c].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ProviderException(ERROR_ACTIVEDIRECTORY_QUERY, ex);
            }
            return res.Count > 0 ? res.ToArray() : new string[0];
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章