【发布时间】:2011-01-29 00:56:14
【问题描述】:
我正在尝试使用 PrincipalContext 通过 Active Directory 服务找出域组件。
我使用很少的参数创建了 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;太多可能会出错。我希望有更好的东西。有人有想法吗?
【问题讨论】:
标签: c# active-directory directoryservices