Class UserData
Public nGuid As Int64 = 0
Public csLogin As String = ""
Public csCommonName As String = ""
Public cUserGroups As Collections.Generic.List(Of String)
End Class
Dim m_cUsersColl As Collections.Generic.SortedList(Of String, UserData)
Sub ReadADUsers()
If Me.dc_address.Text = "" Then
MsgBox("Insert server address")
Exit Sub
End If
If Me.dc_path.Text = "" Then
MsgBox("Insert LDAP path")
Exit Sub
End If
If Me.dc_flt.Text = "" Then
MsgBox("Insert filter")
Exit Sub
End If
m_cUsersColl = New Collections.Generic.SortedList(Of String, UserData)
m_cUsersColl.Clear()
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & dc_address.Text & "/" & dc_path.Text)
Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
oSearcher.PageSize = 5000
Try
Dim nUserCount = 0
oSearcher.Filter = dc_flt.Text
oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("cn")
oSearcher.PropertiesToLoad.Add("sAMAccountName")
oSearcher.PropertiesToLoad.Add("memberOf")
oResults = oSearcher.FindAll()
For Each oResult In oResults
If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
nUserCount = nUserCount + 1
Dim cUserData As UserData = New UserData
cUserData.nGuid = oResult.GetDirectoryEntry().Properties("uid").Value
cUserData.csCommonName = oResult.GetDirectoryEntry().Properties("cn").Value
cUserData.csLogin = oResult.GetDirectoryEntry().Properties("sAMAccountName").Value
cUserData.cUserGroups = New Collections.Generic.List(Of String)
Dim csGroupString As String = ""
Dim nMemberOfCnt = 0
For nMemberOfCnt = 0 To oResult.GetDirectoryEntry().Properties("memberOf").Count - 1
Dim csGroupName As String = CType(oResult.GetDirectoryEntry().Properties("memberOf")(nMemberOfCnt), String)
Dim nStartIdx = csGroupName.IndexOf("=", 1)
Dim nEndIdx = csGroupName.IndexOf(",", 1)
csGroupString = ""
If nStartIdx > 0 And nEndIdx > 0 And nEndIdx > nStartIdx Then
csGroupString = csGroupName.Substring(nStartIdx + 1, nEndIdx - nStartIdx - 1)
End If
If csGroupString <> "" Then
cUserData.cUserGroups.Add(csGroupString)
End If
Next
m_cUsersColl.Add(cUserData.csLogin, cUserData)
End If
Next
MsgBox("Search finished. Found " & nUserCount & " users.")
Catch ex As Exception
MsgBox("Search interrupted with error: " & ex.Message)
End Try
End Sub
|