About getting user data from Active Directory

  • Thread starter Thread starter Dinçer
  • Start date Start date
D

Dinçer

Hi,

I am trying to get user data (email data actually) from Active Directory.
What I exactly want to do is, getting the email address according to
username from the domain.
For example, when I enter my username DINCERM, it should give me
(e-mail address removed) as result.

When I do this:
===
DirectoryEntry entry = new DirectoryEntry(LDAP://DENIZ);

DirectorySearcher searcher = new DirectorySearcher(entry);

searcher.Filter = " (&(objectClass=user)(objectCategory=person)(mail=*))";

===

Then I can get all users with an email address available. But how will I be
able to filter it according to the username?

Thank you..
 
Dinçer,

I'm not that familiar with AD, but when you set the Filter property, it
appears that you are setting a filter to allow all instances of the mail
property to show up. Can't you just add to that filter for the username
property as well?

Hope this helps.
 
For example, when I enter my username DINCERM, it should give me
(e-mail address removed) as result.

DirectoryEntry entry = new DirectoryEntry(LDAP://DENIZ);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = " (&(objectClass=user)(objectCategory=person)(mail=*))";

You'll need to add (samACcountName=DINCERM) to your filter, and also,
you'll need to specify which attributes you want to retrieve:

DirectorySearcher searcher = new DirectorySearcher("LDAP://DENIZ");

searcher.Filter = "
(&(objectClass=user)(objectCategory=person)(samAccountName=DINCERM))";

// add the mail property to the list of props to retrieve
// you can add any others you might be interested in, too!
searcher.PropertiesToLoad.Add("mail");

DirectoryEntry deUser = searcher.FindOne();
if(deUser != null)
{
Console.WriteLine("Your e-mail is: " +
deUser.Properties["mail"].Value.ToString();
}

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top