Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetSAMAccountName(string firstName, string lastName, string domain) {
- string result = string.Empty;
- string connectionPrefix = "LDAP://" + domain;
- using (DirectoryEntry entry = new DirectoryEntry(connectionPrefix))
- using (DirectorySearcher searcher = new DirectorySearcher(entry)) {
- searcher.Filter = string.Format("(&(objectClass=user)(&(sn={1})(givenName={0})))", firstName, lastName);
- SearchResult searchResult = searcher.FindOne();
- if (searchResult == null) {
- throw new NullReferenceException(string.Format("unable to locate the user {0} {1} in the {2} domain", firstName, lastName, domain));
- }
- DirectoryEntry directoryObject = searchResult.GetDirectoryEntry();
- result = directoryObject.Properties["sAMAccountName"].Value as string;
- entry.Close();
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement