我的组织有很多域名。
domainA \ user1,domainB \ user2,domainC \ user3,...
最近,我收到了这个LDAP“ldap.domainA.yyyy.com:1234”,并被告知我应该可以在我的组织内的任何域中查找任何用户。
我使用下面的代码测试过,我只能从domainA(我在域名A下)寻找用户。
根据我的背景:
如何在组织内的任何域中查找任何用户?
我的AD必须以某种方式设置才能实现这一目标吗?我们的AD由我们的系统工程师设置,我不知道设置AD。
class Program
{
static void Main(string[] args)
{
try
{
string currentUser = @"domainA\user1";
Console.WriteLine(currentUser);
Console.WriteLine(currentUser.Substring(currentUser.LastIndexOf(@"\") + 1));
string userName = currentUser.Substring(currentUser.LastIndexOf(@"\") + 1);
string domainPath = "ldap.domainA.yyyy.com:1234";
using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainPath))
{
UserPrincipal userPrincipal = new UserPrincipal(principalContext);
if (userPrincipal != null)
{
userPrincipal.SamAccountName = userName;
PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);
foreach (var found in principalSearcher.FindAll())
{
UserPrincipal userPrincipalFound = found as UserPrincipal;
if (userPrincipalFound != null)
{
Console.WriteLine("SamAccountName:" + userPrincipalFound.SamAccountName);
Console.WriteLine("DisplayName: " + userPrincipalFound.DisplayName);
}
}
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}
}