Active directory (AD) get users as a drop down list by passing text, either Login ID, Email ID or by Starting display Name of User.
Below is the c# code,
1. Adding by exact Email id
2. Adding exact Login user id
3. Name staring example 'Ash*'
internal static
List<Users> GetUserDetails(string sKey)
{
sKey = sKey.Contains("AMAT\\") ?
sKey.Replace("AMAT\\", "") : sKey;
List<Users> lstADUsers = new List<Users>();
try
{
string DomainPath = "LDAP://DC=AMAT,DC=com";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = string.Format("(mail={0})", sKey);
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("usergroup");
search.PropertiesToLoad.Add("displayname");//first name
search.PropertiesToLoad.Add("objectCategory");
search.PropertiesToLoad.Add("objectClass");
search.SizeLimit = 25;
SearchResult result;
SearchResultCollection
resultCol = search.FindAll();
search.Filter = string.Format("(&(|(objectClass=user)(objectClass=group))(anr={0}))", sKey);
//search.Filter
= string.Format("(&(objectClass=user)(givenname={0}))", sKey);
//search.Filter
= string.Format("(&(objectClass = user)(| (displayName = *{0}
*)(givenName = *{0} *)(sn = *{0} *)))", sKey);
search.SizeLimit = 25;
result = null;
resultCol = null;
resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
string UserNameEmailString = string.Empty;
result =
resultCol[counter];
if (result.Properties.Contains("samaccountname") &&
result.Properties.Contains("mail") &&
result.Properties.Contains("displayname"))
{
Users
objSurveyUsers = new Users();
objSurveyUsers.Email = (String)result.Properties["mail"][0];
objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
if
(Convert.ToString((String)result.Properties["objectCategory"][0]).ToLower().Contains("group"))
objSurveyUsers.UserType = "Group";
else if
(Convert.ToString((String)result.Properties["objectCategory"][0]).ToLower().Contains("person"))
objSurveyUsers.UserType = "Person";
else
objSurveyUsers.UserType = "";
lstADUsers.Add(objSurveyUsers);
}
}
}
}
catch (Exception ex)
{
Users objSurveyUsers = new Users();
objSurveyUsers.Email = "Error";
objSurveyUsers.UserName = "Error";
objSurveyUsers.DisplayName =
ex.Message;
lstADUsers.Add(objSurveyUsers);
}
return lstADUsers;
}
--////// sending mail by smtp //////----