2015. 12. 8. 13:20


try

            {

                DirectoryEntry searchRoot = new DirectoryEntry("LDAP://piserver.com",  

                       "test", "Passw0rd");  //서버도메인 정보, 서버의 계정ID, 계정 PW

                DirectorySearcher directorySearcher = new DirectorySearcher(searchRoot);

      

                directorySearcher.Filter = "(&(objectCategory=person)(objectClass=user))";                

                string text = "sAMAccountName"; //ID를 가져오겠다는 뜻이다.

     

                //directorySearcher.PropertiesToLoad.Add("cn");

                directorySearcher.PropertiesToLoad.Add(text);

                directorySearcher.PropertiesToLoad.Add("mail"); //mail을 가져오겠다는 뜻이다.

                directorySearcher.PropertiesToLoad.Add("usergroup"); //usergroup을 가져오겠다는 뜻이다.

                directorySearcher.PropertiesToLoad.Add("displayname"); //표기이름을 가져오겠다는 뜻이다.


                SearchResultCollection resultCol = directorySearcher.FindAll();


                SearchResult result;

string userName = "";

                List<Users> lstADUsers = new List<Users>();

                if (resultCol != null)

                {

                    for (int counter = 0; counter < resultCol.Count; counter++)

                    {

                        string UserNameEmailString = string.Empty;

                        result = resultCol[counter];

                        if (result.Properties.Contains("samaccountname"))

                        {

                            Users objSurveyUsers = new Users();

                           

                            if(result.Properties["samaccountname"] != null)

                                userName = (String)result.Properties["samaccountname"][0];

//해당 항목을 주석처리 한 이유는 해당 항목이 미입력된 경우 에러가 출력되기 때문이다.ID는 반드시 있으므로 ID만 일단 해보는 걸로~

                            //if (result.Properties["displayname"] != null)  

                            //    objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];

                            //if (result.Properties["mail"] != null)

                            //    objSurveyUsers.Email = (String)result.Properties["mail"][0];

                            lstADUsers.Add(objSurveyUsers);

                        }

                    }

                }                            

            }

            catch(Exception ex)

            {

                System.Console.WriteLine("## Get Search AD User List Exception : " + ex.Message);

            }

[참조 : http://www.codeproject.com/Tips/599697/Get-list-of-Active-Directory-users-in-Csharp]

'언어 > C#' 카테고리의 다른 글

[ini] ini파일 읽기 쓰기 기본 코드  (0) 2017.02.14
[Thread]쓰레드 기본 코드  (0) 2016.07.08
[C#] 걸린 시간 체크  (0) 2016.06.08
[C#] ListView item 삭제  (0) 2016.03.22
[C#] ListView 에 Item 추가  (0) 2016.03.22
Posted by 까망후니