using (PrincipalContext Context = new PrincipalContext(ContextType.Domain, DomainURL, UserName, Password))
{
UserPrincipal Account = new UserPrincipal(Context);
Account.GivenName = strFirstName;
Account.Surname = strLastName;
PrincipalSearcher srch = new PrincipalSearcher(Account);
foreach (var principal in srch.FindAll())
{
var p = (UserPrincipal)principal;
String FirstName = p.GivenName;
String LastName = p.Surname;
}
}
如果我使用上面的代码查询 Active Directory,并且在 PrincipalContext 构造函数中传递的用户名(帐户)位于与目标域(要查询的域)不信任的域中,我会收到以下错误。
System.DirectoryServices.AccountManagement.PrincipalServerDownException:无法联系服务器。---> System.DirectoryServices.Protocols.LdapException:LDAP 服务器不可用。
如果将 PrincipalContext 构造更改为,我是否正确?
using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine))
只要客户端在目标域中,代码就会成功执行?
假设第一个带有 UserName 和 Password 的代码被域 A 中的客户端调用,试图在域 B 中搜索用户信息,这里建立上下文失败,因为使用的帐户在域 A 中,与域 B 不信任。
我是否正确假设如果我将 ContextType 更改为 Machine,并且调用代码的客户端位于域 B 中,代码将成功执行?