我有两个信任域名。我在其中一个域中有一个Active Directory帐户,由于已配置的信任,该帐户在另一个域中也具有权限。在我的应用程序中,我需要访问这两个域并列出其中的用户。
我的开发计算机不在上述域中,我无法将其添加到这些域中。 目前,我使用以下代码列出创建帐户的域中的用户:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "192.168.2.11", “Login1”, “pass1”))
{
using (UserPrincipal searchPrincipal = new UserPrincipal(pc))
{
searchPrincipal.Name = "*";
using (PrincipalSearcher searcher = new PrincipalSearcher(searchPrincipal))
{
using (PrincipalSearchResult<Principal> principals = searcher.FindAll())
{
foreach (UserPrincipal principal in principals)
{
Console.WriteLine(principal.Name);
}
}
}
}
}
如果我在不在其中一个域中的计算机上运行代码,我如何调整代码以列出来自两个域的用户?
答案 0 :(得分:1)
你有几个选择。
C# - Searching for users across multiple Active Directory domains
string [] domains = new string [] {“192.168.2.11”,“192.168.2.12”};
for(int i = 0; i < domains.Length; i++)
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domains[i], “Login1”, “pass1”))
{
using (UserPrincipal searchPrincipal = new UserPrincipal(pc))
{
searchPrincipal.Name = "*";
using (PrincipalSearcher searcher = new PrincipalSearcher(searchPrincipal))
{
using (PrincipalSearchResult<Principal> principals = searcher.FindAll())
{
foreach (UserPrincipal principal in principals)
{
Console.WriteLine(principal.Name);
}
}
}
}
}
}