Can I retrieve the domain name and user name by searching Active Directoy using C#


I have a big list of user emails, and I need to get the username and domain name for each one of them.

My organization contains lots of domains and our users log on to their machine using usernames that are different from their email addresses.

Please advise if we can write a C# utility that can search AD using the email of each user, or if we can do it in a simpler way.


Are you on .NET 3.5 ? If so - AD has great new features in .NET 3.5 - check out this article Managing Directory Security Principals in .NET 3.5 by Ethan Wilanski and Joe Kaplan.

One of the big new features is a "PrincipalSearcher" class which should greatly simplify finding users and/or groups in AD.

If you cannot use .NET 3.5, use a DirectorySearcher and specify the e-mail address as your search criteria, and retrieve the user name (which one? There's a gazillion different usernames!):

DirectoryEntry deRoot = new DirectoryEntry("LDAP://cn=Users,dc=yourdomain,dc=com"); DirectorySearcher deSrch = new DirectorySearcher(deRoot); deSrch.SearchScope = SearchScope.Subtree; deSrch.PropertiesToLoad.Add("sn"); // surname = family name deSrch.PropertiesToLoad.Add("givenName"); deSrch.PropertiesToLoad.Add("samAccountName"); deSrch.Filter = string.Format("(&(objectCategory=person)(mail={0}))", emailAddress); foreach(SearchResult sr in deSrch.FindAll()) { // you can access the properties of the search result if(sr.Properties["sn"] != null) { string surname = sr.Properties["sn"][0].ToString(); } // and so on, for all the other properties, too }

Hope this helps!



If that data is all in AD, then you can probably query it using LDAP. In which case, I would recommend DirectorySearcher since you are using .NET.


  • Qt Creator: Design changes not showing when run
  • Task Scheduler from command line
  • Terraform - assume_role_policy - similar but slightly different than standard IAM policy
  • Azure Cosmos DB check if array in field is contained in search array
  • Scroll to anchor link in a Sammy.js project
  • SQL: Find top-rated article in each category
  • Windows service showing a GUI when no user is logged in
  • Autofac with ASP.NET Identity in MVC 5 does not validate Security Stamp in OWIN pipeline
  • C++ face detection/recognition implementations
  • Using publish-composite with Angular-Meteor
  • Categories and SubCategories
  • Qt — pass events to multiple objects?
  • how do you read the ramda docs?
  • How do I manage org and space users in bluemix using cf command line?
  • Is there a better method than to reuse a complicated table in DocBook?
  • Fast way to alphabetically sort the contents of a file in java
  • Capturing HTML Text Input Key press after key has been applied?
  • How to not need user input for install.packages(type = “both”)
  • What dll is needed for Windows.Devices.Geolocation?
  • FTPWebRequest .NET 3.5 vs 4
  • SQL append distinct values from two columns and two tables
  • HALF_PTR Windows data type
  • how to display   in Mozilla using XSL.
  • Cypher - matching two different possible paths and return both
  • Better Indy for Dephi 2007
  • Compare struct to a constant in C
  • Activation Function choice for Neural network
  • HttpListener.IsSupported is false on XP SP3
  • JQuery .html() remove line break on IE 8
  • Enabling DTD support in Sql Server
  • GAE: Way to get reference to an HttpSession from its ID?
  • Web.config system.webserver errors
  • Authentication in Play! and RestEasy
  • Does Mobilefirst provide a provision to access web services directly?
  • Checking free space on FTP server
  • Change Inet root folder for iis 7
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Incrementing object id automatically JS constructor (static method and variable)
  • Updated Ionic CLI but shows previous version (Windows)
  • How to get icons for entities from eclipse?