Active Directory Cross Domain - Group members using PrincipalContext

I am trying to get members of this active directory group using the DirectoryServices.AccouneManagement namespaces classes in C #.

If I have a main constructor of context objects specified for a specific domain, then whenever I access a member from a group located in other domains, I run the following error: "The referral was returned from the server."

Scenario: I have different subdomains in the root domain For example: emea.mycorp.com, asia.mycorp.com, asiapacific.mycorp.com, xyz.mycorp.com

If I run the code below from the domain xyz.mycorp.com, for a group in asiapacific If I specify the server name in the main context object, I could access the group.

private PrincipalContext context = new PrincipalContext(ContextType.Domain, "asiapacific domain server name"); 

If there are users from other domains in my group, such as emea \ abcd, the code below does not work in UserPrincipal:

 GroupPrincipal SearchGroup = GroupPrincipal.FindByIdentity(context, "Dev Team"); GroupName = new List<string>(); foreach (UserPrincipal p in SearchGroup.GetMembers()) { GroupName.Add(p.SamAccountName + " " + p.DistinguishedName + " " + p.Name); } 

So, is there a way to pass the context for the root domain so that the code works regardless of the domain to which the user belongs. I tried below and with no one was lucky:

 private PrincipalContext context = new PrincipalContext(ContextType.Domain, "mycorp.com"); 

or

 private PrincipalContext context = new PrincipalContext(ContextType.Domain, "DC=mycorp,DC=com"); 
+6
source share
1 answer

Try the following:

 new PrincipalContext(ContextType.Domain, "xyz.mycorp.com:3268", "DC=mycorp,DC=com"); 

This will create a PrincipalContext using the global catalog service on your local domain controller (of course, this assumes your local DC is also a GC). This will allow you to search the entire forest.

+13
source

Source: https://habr.com/ru/post/893208/


All Articles