0
votes

Using these 2 examples:

  1. http://blogs.technet.com/b/brad_rutkowski/archive/2008/04/15/c-getting-members-of-a-group-the-easy-way-with-net-3-5-discussion-groups-nested-recursive-security-groups-etc.aspx

    or

  2. Get members of Active Directory Group and check if they are enabled or disabled

I was able to get users from "Domain Users" when running the them on the Domain controller.

However, I was not able to on member machine that belong to the same domain.

I even logon to the member machine as the Domain Administrator

The errors messages:

Example 1

Unhandled Exception: System.Runtime.InteropServices.COMException: The specified domain either does not exist or could not be contacted.

Example 2

Unhandled Exception: System.DirectoryServices.AccountManagement.PrincipalServerDownException: The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.

Can somebody please point me to an example or how to fix this problem ?

Thanks.

1

1 Answers

0
votes

Accounts that are local to a workstation or server are not Active Directory accounts, even if the machine is itself a member of an Active Directory domain. Active Directory APIs generally use LDAP to connect to a domain controller (DC), which will not work for local accounts since there is no DC involved.

Assuming you are working with local users and groups, you can still use the System.DirectoryServices.AccountManagement API to get local users and groups. The DirectoryContext class provides a ContextType property, which you will set to Machine to access local users and groups.

The below code is a simple example that will list all the users on the provided workstation:

string workstationName = null; // null --> localhost
PrincipalContext cxt = new PrincipalContext(ContextType.Machine, workstationName);
foreach (var u in new PrincipalSearcher(new UserPrincipal(cxt)).FindAll())
{
    var userPrincipal = u as UserPrincipal;
    Console.WriteLine(u.Name);
}