Search AD by Email

This code snippet allows you to search Active Directory for a given email address and
returns you the coresponding user object.  

String email = testUser@dev.lan; 

DirectoryEntry LdapConnection = new DirectoryEntry(); 
DirectorySearcher search = new DirectorySearcher(LdapConnection); 
search.SearchScope = SearchScope.Subtree; search.Filter = "(proxyaddresses=smtp:" + email + ")"; 
SearchResult result = search.FindOne();  

if
(result != null)
  { 
DirectoryEntry deDirEnt = result.GetDirectoryEntry();
  
Console.WriteLine("Address Found");
 
DirectoryEntry d = deDirEnt.SchemaEntry;
 
}
  else  { 
Console.WriteLine("Address not found!");
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



 

Dilbert of the day