我有一个 VB 应用程序 (.NET 4.0),用户在其中选择他们拥有的 AD 组,然后可以将预定义列表中的用户添加到该组。从 AD 中提取的组和从 Oracle 中提取的用户都是现有的 AD 用户。
您将看到三个带注释的代码块,我已经尝试了所有三个并得到“COMException is unhandled by user code: Unspecified error”。
<WebMethod()> _
Public Shared Function AddDirectReport(ByVal User As String, ByVal Group As String) As String
Dim GroupMembers As List(Of String) = LoadGroupMembers(Group)
If GroupMembers.Contains(User) Then
Return "USER " & User & " IS ALREADY IN GROUP " & Group
End If
Dim SearchRoot As New DirectoryEntry("[LDAP Path]")
Dim GroupSearcher As New DirectorySearcher
With GroupSearcher
.SearchRoot = SearchRoot
.Filter = "(&(ObjectClass=Group)(CN=" & Group & "))"
End With
Dim UserSearcher As New DirectorySearcher
With UserSearcher
.SearchRoot = SearchRoot
.Filter = "(&(ObjectClass=Person)(CN=" & User & "))"
End With
Dim g As DirectoryEntry = GroupSearcher.FindOne.GetDirectoryEntry
Dim u As DirectoryEntry = UserSearcher.FindOne.GetDirectoryEntry
'With u
' .Properties("memberof").Add(g)
' .CommitChanges()
'End With
'With g
' .Properties("member").Add(u)
' .CommitChanges()
'End With
'With g
' .Properties("members").Add(u)
' .CommitChanges()
'End With
Return "Success?"
End Function