public static DirectoryEntry FindUser(string userName) { if (string.IsNullOrEmpty(LoginDomain) || !UseWindowsCreds && (string.IsNullOrEmpty(DomainName) || string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))) { if (EditADPrefs.Execute() != System.Windows.Forms.DialogResult.OK) { return(null); } } try { DirectoryEntry dom = null; if (UseWindowsCreds) { dom = new DirectoryEntry("LDAP://" + DomainName); } else { dom = new DirectoryEntry("LDAP://" + DomainName, LoginDomain + @"\" + Username, Password, AuthenticationTypes.None); } using (DirectorySearcher dsSearcher = new DirectorySearcher(dom)) { dsSearcher.Filter = string.Format("(&(objectClass=user)(|(cn={0})(samaccountname={0})))", userName); dsSearcher.PropertiesToLoad.Add("ThumbnailPhoto"); SearchResult result = dsSearcher.FindOne(); if (result == null || string.IsNullOrEmpty(result.Path)) { return(null); } if (UseWindowsCreds) { return(new DirectoryEntry(result.Path)); } return(new DirectoryEntry(result.Path, LoginDomain + @"\" + Username, Password, AuthenticationTypes.None)); } } catch (Exception e) { MessageBox.Show(string.Format("Failed to search for user.\r\n\r\nError was:\r\n{0}", e.Message), "A/D Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); }
private void miEditADPrefs_Click(object sender, EventArgs e) { EditADPrefs.Execute(); }