/// <summary> /// Method to validate if a user exists in the AD. /// </summary> /// <param name="UserName"></param> /// <returns></returns> public bool UserExists(string UserName) { DirectoryEntry de = ADConnect.GetDirectoryEntry(); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))"; SearchResultCollection results = deSearch.FindAll(); if (results.Count == 0) { return(false); } else { return(true); } }
/// <summary> /// Method that disables a user account in the AD and hides user's email from Exchange address lists. /// </summary> /// <param name="EmployeeID"></param> public void DisableAccount(string EmployeeID) { DirectoryEntry de = ADConnect.GetDirectoryEntry(); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = "(&(objectCategory=Person)(objectClass=user)(employeeID=" + EmployeeID + "))"; ds.SearchScope = SearchScope.Subtree; SearchResult results = ds.FindOne(); if (results != null) { DirectoryEntry dey = ADConnect.GetDirectoryEntryAtPath(results.Path); int val = (int)dey.Properties["userAccountControl"].Value; dey.Properties["userAccountControl"].Value = val | 0x0002; dey.Properties["msExchHideFromAddressLists"].Value = "TRUE"; dey.CommitChanges(); dey.Close(); } de.Close(); }