我正在尝试使用PrincipalContext通过Active Directory Services查找域组件。
我使用很少的参数创建了PrincipalContext:
PrincipalContext theContext = new PrincipalContext(ContextType.Domain);theContext恢复正常。我可以在上面查询很多东西,并得到预期的结果。但我真正想做的是:
Console.WriteLine("Domain Component: " + theContext.Container);根据MSDN的说法,这只是“获取在构造函数的容器参数中指定的值”。因为我没有传入任何东西,所以我什么也得不到。
但理论容器具有域组件,您可能需要使用或创建的任何可分辨名称都需要域组件。我特别希望在我知道将会出现的组织单位中创建一个新用户。但是,因为我不知道域组件,所以我不能创建可分辨名称。我还没有看到任何对相对路径的支持。
我认为最好的选择是搜索任何用户,然后获得其可分辨名称并去掉"dc“部分。
var searchUser = new UserPrincipal(theContext);
var searcher = new PrincipalSearcher(searchUser);
Principal aUser = searcher.FindOne();
if (aUser != null)
{
string dn = aUser.DistinguishedName;
Console.WriteLine(dn.Substring(dn.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase)));
}这感觉像是一个糟糕的hack;太多的东西可能会出错。我希望有更好的东西。有谁有主意吗?
发布于 2011-01-29 13:11:37
为了获得命名上下文,您应该绑定到RootDSE。RootDSE提供了许多有关目录服务器的有用信息。其中,defaultNamingContext属性存储域的可分辨名称,这正是您想要的。
要绑定到当前登录用户的域的RootDSE,您可以使用serverless binding。要在当前登录的用户域的RootDSE上执行无服务器绑定,绑定字符串应如下所示
LDAP://RootDSE下面是使用DirectoryEntry获取它的方法
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string domainContext = rootDSE.Properties["defaultNamingContext"].Value as string;https://stackoverflow.com/questions/4834393
复制相似问题