【问题标题】:Use PrincipalSearcher to get DFS shares使用 PrincipalSearcher 获取 DFS 共享
【发布时间】:2013-04-02 19:51:42
【问题描述】:

我正在尝试以 Windows 形式将代码从 VB 改编为 C#。我仍在尝试总体上了解 DFS 的想法,以及如何从 Windows 窗体中操作它。

VB 使用GetObject("LDAP://RootDSE") 函数在Active Directory 中搜索使用DirectorySearcher 的共享。我已经调整了使用同一对象的其他函数,以从用户 ID 返回 UserPrincipal,并检查组是否已存在(使用 GroupPrincipal)。这些通常是这样的:

public static UserPrincipal GetUserPrincipal(string userId) {
    PrincipalContext context = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = new UserPrincipal(context);
    user.Name = userId;
    PrincipalSearcher searcher = new PrincipalSearcher(user);
    return searcher.FindOne() as UserPrincipal;
}

但是,我找不到任何包含我正在使用的关键字的文档,但我正在尝试获取属于 DFS 命名空间的目录列表(我认为)。

这是 VB 中的(修改后的)代码:

Public Function GetDfsNamespaces() As List(Of String)
    Dim objRootDSE = GetObject("LDAP://RootDSE")
    Dim domain As String = objRootDSE.Get("DefaultNamingContext")
    Dim entry As New DirectoryEntry("LDAP://CN=DFs-Configuration,CN=System," & domain)
    Dim searcher As New DirectorySearcher(entry)
    searcher.PropertiesToLoad.Add("cn")
    searcher.Filter = "(objectClass=msDFS-NamespaceAnchor)"
    searcher.SearchScope = SearchScope.Subtree
    Dim results As SearchResultCollection = searcher.FindAll()
    Dim strResults As New List(Of String)
    For Each result In results
        strResults.Add(result.Properties("cn")(0))
    Next
    return strResults
End Function

我尝试查看 UserPrincipalGroupPrincipalComputerPrincipal 的来源,但无法弄清楚如何扩展 Principal 对象以获取目录或其他内容。

【问题讨论】:

    标签: c# winforms active-directory microsoft-distributed-file-system


    【解决方案1】:

    前两行应该是这样的:

            string domain;
            using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))
            {
                domain = rootDSE.Properties["defaultNamingContext"].Value.ToString();
            }
    

    剩下的代码应该可以直接转换。

    【讨论】:

    • 哇,这很简单。我读过一些关于 PrincipalSearcher 的东西是要走的路,因为它更快?它随 .NET 3.5 一起提供,你认为有办法用新对象做到这一点吗?
    • 这些实际上通常比较慢。坚持使用 DirectoryEntry / DirectorySearcher。
    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多