【问题标题】:Search LDAP for user permissions with VB.NET使用 VB.NET 在 LDAP 中搜索用户权限
【发布时间】:2012-06-13 12:21:08
【问题描述】:

我正在尝试使用 VB.NET 从 Active Directory 中获取一些信息。 我有一个用户的“primaryGroupID”,在这种情况下是 2096。 如何通过 VB.NET 获得该组的 CN?

最终,我需要做的是找到一个用户所属的组列表(包括属于另一个组的组)。我已经有一个函数可以获取除主要组之外的主要组,以及另一个返回主要组 ID 的函数。两者都在下面详细说明。

Public Function getUserGroups(ByVal Username)
    Dim grupos As New ArrayList()
    Try
        Dim Entry As New System.DirectoryServices.DirectoryEntry(ldapPath, ldapAdminUser, ldapAdminPass)
        Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
        Searcher.SearchScope = DirectoryServices.SearchScope.Subtree
        Searcher.Filter = "(&(objectcategory=user)(SAMAccountName=" & Username & "))"
        Dim res As SearchResult = Searcher.FindOne

        For i = 0 To res.Properties("memberOf").Count() - 1
            grupos.Add(res.Properties("memberOf")(i).ToString)
        Next
    Catch ex As Exception
    End Try
    Return grupos
End Function


Public Function GetUserPrimaryGroupID(ByVal user As String) As String
    Dim grupoID As String = ""
    Try
        Dim Entry As New System.DirectoryServices.DirectoryEntry(ldapPath, ldapAdminUser, ldapAdminPass)
        Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
        Searcher.SearchScope = DirectoryServices.SearchScope.Subtree
        Searcher.Filter = "(&(objectcategory=user)(SAMAccountName=" & user & "))"
        Dim res As SearchResult = Searcher.FindOne

        For i = 0 To res.Properties("primaryGroupID").Count() - 1
            grupoID = (res.Properties("primaryGroupID")(i).ToString) 'Esto devuelve la ruta "CN" del grupo
            'grupoID = (res.Properties("primaryGroupID")(i).ToString)
            'Dim de As DirectoryEntry = New DirectoryEntry("LDAP://" + res.Properties("primaryGroupID")(i).ToString())
        Next
    Catch ex As Exception
    End Try
    Return grupoID
End Function

【问题讨论】:

    标签: vb.net active-directory ldap


    【解决方案1】:

    这里有一个 VBScript 示例 - http://support.microsoft.com/kb/297951

    基本上,主要组 ID 是组的 RID(SID 的最后一个组成部分)。因此,要查找组,请将域 SID 和主组 ID 连接在一起。

    【讨论】:

    • 感谢布赖恩的回复。我不知道你可以这样。我尝试了他们在网站上提到的代码,但我找不到让它在 VB.Net 中工作的方法。我对VBS不是很熟悉。在 VB.NET 中是否有任何其他示例可以做同样的事情?我还尝试从 VBS 中“翻译”代码,但没有成功 :(
    • 这是一个 C# 示例 - stackoverflow.com/questions/1179858/…
    • 你好,布莱恩。您的提示绝对有帮助!我仍然在为一个问题而苦苦挣扎,因为我的功能在域中的 PC 上工作,但它们不在域中的 PC 上。奇怪....一旦我整理好代码,我就会在 VB.Net 中发布代码,这样人们就可以完成它。感谢您的帮助,如果您知道如何解决域问题,请告诉我! ^^
    • 我希望问题出在凭据上,或者您如何将服务器/搜索根信息传递给 DirectoryEntry 的构造函数。
    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多