private int CompareLDAPUsers(LDAPUserObject x, LDAPUserObject y) { if (x == null) { if (y == null) { return(0); } else { return(-1); } } else { if (y == null) { return(1); } else { return(string.Compare(x.DisplayName, y.DisplayName)); } } }
public IEnumerable <LDAPUserObject> GetUsers(string filter = "") { List <LDAPUserObject> users = null; LDAPUserObject ldapUser; using (DirectoryEntry de = GetDirectoryEntry()) { using (DirectorySearcher deSearcher = new DirectorySearcher(de)) { if (string.IsNullOrEmpty(filter)) { deSearcher.Filter = "(&(objectCategory=person))"; } else { deSearcher.Filter = filter; } deSearcher.PropertiesToLoad.Add("samAccountName"); deSearcher.PropertiesToLoad.Add("displayName"); using (SearchResultCollection results = deSearcher.FindAll()) { users = new List <LDAPUserObject>(); foreach (SearchResult result in results) { ldapUser = new LDAPUserObject(); ldapUser.UserName = result.Properties["samAccountName"][0].ToString(); if (result.Properties["displayName"].Count > 0) { ldapUser.DisplayName = result.Properties["displayName"][0].ToString(); } users.Add(ldapUser); } users.Sort(CompareLDAPUsers); return(users); } } } }
/// <summary> /// Returns a list of users and their password expiration date. /// </summary> /// <returns></returns> public IEnumerable <LDAPUserObject> GetUsersPasswordExpiration(string filter = "") { List <LDAPUserObject> users = null; LDAPUserObject ldapUser; using (DirectoryEntry de = GetDirectoryEntry()) { using (DirectorySearcher deSearcher = new DirectorySearcher(de)) { if (filter == "") { deSearcher.Filter = "(&(objectCategory=person))"; } else { deSearcher.Filter = filter; } deSearcher.PropertiesToLoad.Add("samAccountName"); deSearcher.PropertiesToLoad.Add("displayName"); deSearcher.PropertiesToLoad.Add("pwdLastSet"); using (SearchResultCollection results = deSearcher.FindAll()) { users = new List <LDAPUserObject>(); foreach (SearchResult result in results) { ldapUser = new LDAPUserObject(); ldapUser.UserName = result.Properties["samAccountName"][0].ToString(); ldapUser.DisplayName = result.Properties["displayName"][0].ToString(); ldapUser.PasswordLastSet = DateTime.FromFileTime((long)result.Properties["pwdLastSet"][0]); users.Add(ldapUser); } users.Sort(CompareLDAPUsers); return(users); } } } }