0
我想从VB.NET获取Active Directory中的一些信息。 我有一个用户的“primaryGroupID”,在这种情况下是2096。 如何使用VB.NET获得该组的CN?用VB.NET搜索用户权限的LDAP
最终,我需要做的是找到用户所属组的列表(包括属于另一个组的组)。我已经有一个函数可以获取除主要组以外的主要组,另一个函数可以返回主要组的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
感谢Brian的回复。我不知道你可以这样做。 我试过他们在网站上提到的代码,我找不到一种方法使它在VB.Net中工作。我对VBS不是很熟悉。 有没有其他的例子在VB.NET中做同样的事情?我也试过“翻译”VBS的代码,没有运气:( – guillerone
这是一个C#示例 - http://stackoverflow.com/questions/1179858/can-you-find-an-active-directory-users-primary-group -in-c –
再次感谢Brian,你的提示很有帮助!我仍在努力解决一个问题,因为我的功能在域中的PC上工作,但它们不在不在域中的PC上。 ... 我会在VB.Net中发布代码,以便人们可以完成它。感谢您的帮助,如果您知道如何解决域问题,请告诉我!^^ – guillerone