Active Directory problem - search.FindAll()

G

Guest

I'm able to connect to AD and retrieve the value of mail for one user (for example, "jones") successfully, with the following code

public void lookupOne(

String domainAndUsername = domain + @"\" + username
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd)

tr
{
Object obj = entry.NativeObject

DirectorySearcher search = new DirectorySearcher(entry)

search.Filter = "(SAMAccountName=" + username + ")"

search.PropertiesToLoad.Add("mail")

SearchResult result = search.FindOne()

_path = result.Path

_filterAttribute = (String)result.Properties["mail"][0]

Label1.Text = _path

Label2.Text = _filterAttribute

catch (Exception ex){



But, I want to retrieve the mail addresses for all users whose names are "jon*". The following code doesn't work - it throws an exception. Can someone tell me what I need to do here

public void lookupAll(


String domainAndUsername = domain + @"\" + username
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd)

tr
{
Object obj = entry.NativeObject

DirectorySearcher search = new DirectorySearcher(entry);

search.PropertiesToLoad.Add("mail")

search.Filter = "(&(objectclass=user)(sAMAccountName=" + UserName + "))"

SearchResultCollection resultCollection = search.FindAll()

Label1.Text = resultCollection.Count.ToString()


catch (Exception ex) {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top