Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Active Directory searching

Posted on 2005-05-01
7
Medium Priority
?
298 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:Razzie_
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:zupi5
ID: 13904484
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
 
LVL 8

Author Comment

by:Razzie_
ID: 13904737
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
 
LVL 6

Accepted Solution

by:
zupi5 earned 1800 total points
ID: 13908106
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Assisted Solution

by:ihenry
ihenry earned 200 total points
ID: 13908279
>> 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
 
LVL 8

Author Comment

by:Razzie_
ID: 13957626
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
 
LVL 8

Author Comment

by:Razzie_
ID: 14228830
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question