[C#] Active Directory 사용자 리스트 가져오기
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]