Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross-domain Active Directory queries

Status
Not open for further replies.

huckfinn19

Programmer
May 16, 2003
90
CA
Hi all,

I am using C# 2.0.

Here is my active directory setup:

MAIN_DIRECTORY (root)
\ SUBDOMAIN1 -> [user1, user2]
\ SUBDOMAIN2 -> [user3, user4]

From a machine in the SUBDOMAIN1, I want to query users from both subdomains.

I am using the following code:
Code:
DirectoryEntry oDirEntry = new DirectoryEntry(string.Format("LDAP://{0}", SUBDOMAIN_1_OR_2));
string sSearchFilter = string.Format("(&(objectClass=user)(SAMAccountName={0}))", SOME_USERNAME);
DirectorySearcher oDirSearcher = new DirectorySearcher(oDirEntry, sSearchFilter);
SearchResultCollection aoUsers = oDirSearcher.FindAll();
This works fine when I explicitely query SUBDOMAIN1 with the users that are in that domain (user1, user2, ...) or SUBDOMAIN2 with the users form that domain (user3, user4, ...).

My problem is that at runtime, I don't know what subdomain the users are from and I don't want to query all subdomains one after the other to find user information.

Is these a way to query the entire directory (root) for users without knowing what subdomains they are in? If not, is there another better way to do this?

Thanks.
 
Not sure if this helps but :

Code:
DirectoryEntry o=new DirectoryEntry("LDAP://"+context);
     DirectorySearcher sr  = new DirectorySearcher(o);
     sr.Filter= ("(&(objectclass=user)(objectcategory=Person))");
     sr.SearchScope=SearchScope.Subtree;
     foreach(SearchResult srres in  sr.FindAll()) 
     {
         DirectoryEntry ro=srres.GetDirectoryEntry();
         if(ro.SchemaClassName.ToLower()=="user") 
         {
            ActiveDs.IADsUser iuser=(ActiveDs.IADsUser)ro.NativeObject;
           Console.WriteLine(iuser.Get("sAMAccountName").ToString());
        }
    }
    o.Close();
 
Hi, thanks for your reply, but the code you sent does the same as mine. I didn't show how I iterate through the results of the FindAll() method, but if you check, the previous calls are essentially the same. The SearchScope property is also set to "SubTree" by default.

any other suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top