Advertisement
Guest User

Untitled

a guest
May 4th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. public static string GetSAMAccountName(string firstName, string lastName, string domain) {
  2. string result = string.Empty;
  3. string connectionPrefix = "LDAP://" + domain;
  4. using (DirectoryEntry entry = new DirectoryEntry(connectionPrefix))
  5. using (DirectorySearcher searcher = new DirectorySearcher(entry)) {
  6.  
  7. searcher.Filter = string.Format("(&(objectClass=user)(&(sn={1})(givenName={0})))", firstName, lastName);
  8. SearchResult searchResult = searcher.FindOne();
  9.  
  10. if (searchResult == null) {
  11. throw new NullReferenceException(string.Format("unable to locate the user {0} {1} in the {2} domain", firstName, lastName, domain));
  12. }
  13. DirectoryEntry directoryObject = searchResult.GetDirectoryEntry();
  14. result = directoryObject.Properties["sAMAccountName"].Value as string;
  15. entry.Close();
  16. }
  17. return result;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement