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

Posted by BrianLy on Stack Overflow See other posts from Stack Overflow or by BrianLy
Published on 2010-03-29T03:09:11Z Indexed on 2010/03/29 3:13 UTC
Read the original article Hit count: 423

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about CSharp

Related posts about dotnet