Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct method to search for AD user by email address from .NET

I'm having some issues with code that is intended to find a user in Active Directory by searching on their email address. I have tried 2 methods but I'm sometimes finding that the FindOne() method will not return any results on some occasions. If I look up the user in the GAL in Outlook I see the SMTP email address listed.

My end goal is to confirm that the user exists in AD. I only have the email address as search criteria, so no way to use first or last name.

Method 1: Using mail property:

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(mail=" + email + ")";
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();

Method 2: proxyAddresses property:

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(proxyAddresses=SMTP:" + email + ")"; // I've also tried with =smtp:
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();

I've tried changing the case of the email address input but it still does not return a result. Is there a problem here with case sensitivity? If so, what is the best way to resolve it?

like image 807
Brian Lyttle Avatar asked Mar 29 '10 03:03

Brian Lyttle


1 Answers

I've found that using SysInternals ADExplorer is great for testing out/debugging Active Directory queries. As you can build the queries and run them against Active Directory you can see the results as well as easily view objects and see all their properties...

like image 195
davidsleeps Avatar answered Oct 29 '22 00:10

davidsleeps