如何在非域控制器上获取Active Directory用户

本文关键字:获取 Active Directory 用户 控制器 域控 | 更新日期: 2023-09-27 18:22:02

使用以下两个示例:

  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. 获取Active Directory组的成员并检查他们是否已启用或禁用

当在域控制器上运行"域用户"时,我能够从"域用户"中获取用户。

然而,我无法在属于同一域的成员计算机上运行。

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

错误信息:

示例1

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

示例2

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

有人能给我举个例子吗?或者如何解决这个问题?

谢谢。

如何在非域控制器上获取Active Directory用户

工作站或服务器的本地帐户不是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);
}