Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# - need to search for email address in Active Directory

1,280 views
Skip to first unread message

Always Learning

unread,
Dec 2, 2007, 1:19:01 AM12/2/07
to
Hello all,

I'd very much appreciate a snippet of C# code showing me how to search for
an SMTP address in Active Directory. I need to be able to pass an address to
the routine and have it return whether the address is in Active Directory or
not.

I'm new to C# and rusty with regards to programming so any help is
appreciated.

I have figured out that I need to be using System.DirectoryServices and that
I'll be searching for a ProxyAddress. And that I need to create a Directory
Searcher, but past that I'm clueless.

Thanks in advance for any pointers!


wolfename...@discussions.microsoft.com

unread,
Dec 20, 2007, 5:16:01 PM12/20/07
to
Yes So would I I can not believe how many different ways there are to access
LDAP (Active Directory) and how many of them don't work as explain all over
MS site.

Did you ever get a answer for this?

Joe Kaplan

unread,
Dec 20, 2007, 6:16:58 PM12/20/07
to
As I recall I replied to this in one of the other newsgroups he posted in
(probably microsoft.public.adsi.general).

The problem with providing a sample is that there isn't enough information
provided here to guarantee it will work. You need to know stuff about the
environment it will run it, the security context that will be used to query
the directory, whether you need to talk to a single domain or search across
the forest in the global catalog, etc.

The actual LDAP query isn't hard and can be done in .NET with either
System.DirectoryServices or System.DirectoryServices.Protocols, depending on
your needs.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
"wol...@ameritech.net" <wolfename...@discussions.microsoft.com> wrote
in message news:E5EDA9E9-C392-453F...@microsoft.com...

Marc Scheuner

unread,
Dec 21, 2007, 3:40:46 PM12/21/07
to
>I'd very much appreciate a snippet of C# code showing me how to search for
>an SMTP address in Active Directory. I need to be able to pass an address to
>the routine and have it return whether the address is in Active Directory or
>not.

I tried to put together the core of such a function for you - mind
you, there's still a lot of room for improvement, and you might need
to tweak it a bit to suit your requirements - but it should get you
started, I hope! See the comments in the source:

using System;
using System.DirectoryServices;

namespace FindByEMail
{
class Program
{
static void Main(string[] args)
{
// pass in the e-mail to search for as the parameter for
this tool
if (args.Length <= 0)
{
Console.WriteLine("No e-mail specified!");
return;
}

string emailAddress = args[0];

// define the root of your search - by default, the
top-level of your domain
DirectoryEntry rootEntry = new
DirectoryEntry("LDAP://dc=bluenose,dc=local");

// define a searcher
DirectorySearcher searcher = new
DirectorySearcher(rootEntry);

// set the filter for the searcher - the LDAP filter
syntax is a bit
// odd at times; for details see
// http://msdn2.microsoft.com/en-us/library/aa746475.aspx
// http://www.selfadsi.org/ldapfilter.htm
searcher.Filter = string.Format("(mail={0})",
emailAddress);

// define the properties you want in the search results
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add("l");
searcher.PropertiesToLoad.Add("department");
searcher.PropertiesToLoad.Add("title");

// search scope - here: search the whole subtree below
your root
searcher.SearchScope = SearchScope.Subtree;

// define the sort order for the results
searcher.Sort = new SortOption("sn",
SortDirection.Ascending);

// find the results
SearchResultCollection results = searcher.FindAll();

// iterate through the results
foreach (SearchResult result in results)
{
// extract the resulting values - be careful: if a
property has no value
// associated with it, (result.Properties[....]) will
be null! Check for that!
// Also - a property can contain more than one result
value - but make sure
// there's at least one! If there's more than one, you
might need to write
// some additional code to deal with that...
string title = string.Empty;
if (result.Properties["title"] != null &&
result.Properties["title"].Count > 0)
{
title = result.Properties["title"][0].ToString();
}

string name = string.Empty;
if (result.Properties["name"] != null &&
result.Properties["name"].Count > 0)
{
name = result.Properties["name"][0].ToString();
}

string department = string.Empty;
if (result.Properties["department"] != null &&
result.Properties["department"].Count > 0)
{
department =
result.Properties["department"][0].ToString();
}

string location = string.Empty;
if (result.Properties["l"] != null &&
result.Properties["l"].Count > 0)
{
location = result.Properties["l"][0].ToString();
}

// output your result(s)
Console.WriteLine("{0} {1} (Dept. {2}) in {3}", title,
name, department, location);
}
}
}
}


HTH
Marc

0 new messages