【问题标题】:Find the domain component from a PrincipalContext从 PrincipalContext 中查找域组件
【发布时间】: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


    【解决方案1】:

    为了获得命名上下文,你应该绑定到RootDSE。 RootDSE 提供了很多有用的目录服务器信息。其中defaultNamingContext属性存放的是域名的专有名称,这就是你想要的。

    要绑定到当前登录用户所在域的RootDSE,可以使用serverless binding。要在当前登录用户域的 RootDSE 上进行无服务器绑定,绑定字符串应如下所示

    LDAP://RootDSE
    

    这里是使用DirectoryEntry获取它的方法

    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
    string domainContext = rootDSE.Properties["defaultNamingContext"].Value as string;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2018-11-10
      • 2013-05-01
      相关资源
      最近更新 更多