如何在非域控制器

时间:2015-11-14 09:13:23

标签: c# active-directory

使用这两个例子:

  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

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

  3. 我能够从" Domain Users"在域控制器上运行它们时。

    但是,我无法在属于同一域的成员计算机上。

    我甚至以域管理员身份登录到成员计算机

    错误消息:

    示例1

      

    未处理的异常:System.Runtime.InteropServices.COMException:指定的域不存在或无法联系。

    示例2

      

    未处理的异常:System.DirectoryServices.AccountManagement.PrincipalServerDownException:无法联系服务器。 ---> System.DirectoryServices.Protocols.LdapException:LDAP服务器不可用。

    有人可以请一个例子或如何解决这个问题吗?

    感谢。

1 个答案:

答案 0 :(得分:0)

工作站或服务器本地的帐户不是Active Directory帐户,即使计算机本身是Active Directory域的成员也是如此。 Active Directory API通常使用LDAP连接到域控制器(DC),这对于本地帐户不起作用,因为不涉及DC。

假设您正在使用本地用户和群组,您仍然可以使用System.DirectoryServices.AccountManagement API来获取本地用户和群组。 DirectoryContext类提供ContextType属性,您将设置为Machine以访问本地用户和组。

以下代码是一个简单示例,它将列出所提供工作站上的所有用户:

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);
}