public ADUserDetail GetUserByFullName(String userName) { try { _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Filter = "(&(objectClass=user)(cn=" + userName + "))"; SearchResult results = directorySearch.FindOne(); if (results != null) { DirectoryEntry user = new DirectoryEntry(results.Path, "monin.jose", "Password.1"); return(ADUserDetail.GetUser(user)); } else { return(null); } } catch (Exception ex) { return(null); } }
public static void getAllUser() { _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Filter = "(&(objectCategory=person)(objectClass=user))"; var results = directorySearch.FindAll().Cast <SearchResult>(); if (results != null) { List <ADUserDetail> users = new List <ADUserDetail>(); List <UserDetail> usersList = new List <UserDetail>(); System.Threading.Tasks.Parallel.ForEach(results, sr => { DirectoryEntry user = new DirectoryEntry(sr.Path); ADUserDetail userAd = ADUserDetail.GetUser(user); if (userAd != null && userAd.EmailAddress != null && userAd.EmailAddress.Trim() != "") { if (userAd.FirstName.ToLower().Contains("monin")) { } Console.WriteLine(userAd.EmailAddress); UserDetail userDetail = userAd.copyToUserDetails(); if (userAd.Manager != null) { userDetail.ManagerEmail = userAd.Manager.EmailAddress.ToLower(); } usersList.Add(userDetail); users.Add(userAd); } }); /*foreach (SearchResult sr in results) * { * DirectoryEntry user = new DirectoryEntry(sr.Path); * ADUserDetail userAd = ADUserDetail.GetUser(user); * if (userAd != null && userAd.FirstName != null && userAd.EmailAddress != null && userAd.EmailAddress.Trim() != "" && userAd.FirstName.Trim() != "") * { * if (userAd.FirstName.ToLower().Contains("monin")) * { * * } * Console.WriteLine(userAd.EmailAddress); * UserDetail userDetail = userAd.copyToUserDetails(); * if (userAd.Manager != null) * { * //userDetail.ManagerEmail = userAd.Manager.EmailAddress.ToLower(); * } * usersList.Add(userDetail); * users.Add(userAd); * } * }*/ using (var ctx = new UserDb()) { //var stud = new UserDetail() { StudentName = "Bill" }; for (int i = 0; i < usersList.Count; i++) { ctx.UserDetails.Add(usersList[i]); } ctx.SaveChanges(); } //DirectoryEntry user = new DirectoryEntry(results.Path, "monin.jose", "Password.1"); //return ADUserDetail.GetUser(user); } //DirectoryEntry entry = new DirectoryEntry("LDAP://domainName"); //DirectorySearcher dsearch = new DirectorySearcher(entry); //dsearch.Filter = "(&(objectClass=user))"; //SearchResult result = dsearch.FindOne(); }