Active Directory searching

Hey people,

I need help accomplishing doing a search in the Active Directory. I need to check if a user exists in the AD with a specified email address.
Since I have no knowledge at all of this, I might as well ask it here :)

What I did was:

DirectoryEntry entry = null; DirectorySearcher mySearcher = null;

entry = new DirectoryEntry();
mySearcher = new DirectorySearcher(entry);

mySearcher.Filter = .....;


How can I check if a user exists when I only have an email address? Do I need to take on a completely different approach?

Regards,

Razzie
LVL 8
Razzie_Asked:
Who is Participating?
 
zupi5Commented:
you sohuld be able to query the domain, with domain address for example: LDAP://domain.com.  
Domain data is replicated to all controllers within a domain.

this function should return all of active directory domains:

private StringCollection GetDomainList()
{
      StringCollection domainList = new StringCollection();
      try
      {
            DirectoryEntry en = new DirectoryEntry("LDAP://");
            // Search for objectCategory type "Domain"
            DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
            SearchResultCollection coll = srch.FindAll();
            // Enumerate over each returned domain.
            foreach (SearchResult rs in coll)
            {
                  ResultPropertyCollection resultPropColl = rs.Properties;
                  foreach( object domainName in resultPropColl["name"])
                  {
                        domainList.Add(domainName.ToString());
                  }
            }
      }
      catch (Exception ex)
      {
            Trace.Write(ex.Message);
      }
      return domainList;
}                  
0
 
zupi5Commented:
Hi,

this gunction should find a user with a specific email, just change "LDAP://server.addreess" to the address of your domain controler.


private bool UserExistsByEmail(String email)

{

    DirectoryEntry entry = new  DirectoryEntry("LDAP://server.addreess");

    try

    {
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(&(objectCategory=user)(mail=" + email + "))";
        search.PropertiesToLoad.Add("displayName");
        SearchResult result = search.FindOne();

        if( result != null )

        {
          return true;
        }
        else

        {
          return false;
        }
    }
    catch( Exception ex )

    {

        string debug = ex.Message;

        return false;

    }

}
0
 
Razzie_Author Commented:
Hello zupi5,

Thanks for your reply and yes, I had figured that out (sorry for not making that clear). However, what to do when I don't know the domain controller? And, can there be more than 1 domain controller available?

If yes, how do I get all the domains?
If not, how do I get the name of the domain controller from a machine?

Thanks,

Razzie
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ihenryCommented:
>> If yes, how do I get all the domains?
If you are in the domain, you don't have to know all domain names for just to query to your AD. There's a serverless binding method you can use which is not required domain controller to be specified in the binding string. For example,

DirectoryEntry root = new DirectoryEntry();
root.Path = "LDAP://rootDSE";
String dnsHost = (String) root.Properties["defaultNamingContext"].Value;

String ldapPath = String.Format( "LDAP://{0}", dnsHost );
DirectoryEntry searchRoot = new DirectoryEntry();
searchRoot.Path = ldapPath;

DirectorySearcher searcher = new DirectorySearcher( searchRoot );
...
...
0
 
Razzie_Author Commented:
Thanks for the replies guys. Just to let you know, I don't have a lot of time to work on this project so it may take a while before I can try the solutions, I will keep you updated.
0
 
Razzie_Author Commented:
Didn't forget about this question, but this project is 'on hold' for some time. First results indicate that zupi5's solution may work well. I'll accept it as the solution since waiting any longer may not be fair :)

Thanks all, and sorry for the late PAQ.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.