1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
'ldapServerName = adresse IP du serveur LDAP
'userName du type uid=toto,cn=users,dc=world, dc=localnet
_targetOu l'OU où se trouve les elements recherchés
Public Function Connect(ByVal ldapServerName As String, ByVal userName As String, ByVal password As String) As LdapConnection
'to use LDAP calls, you must first connect to the directory.
Try
'creation d'une connection LDAP
Dim id As New LdapDirectoryIdentifier(ldapServerName, False, False)
Dim nc As New NetworkCredential(userName, password)
connection = New LdapConnection(id)
With connection
.Credential = nc
.AuthType = AuthType.Basic
.Timeout = New TimeSpan(0, 5, 60)
.SessionOptions.ProtocolVersion = 3
End With
Return connection
Catch e As Exception
Console.WriteLine("\r\nErreur:\r\n\t" + e.GetType().Name + ":" + e.Message)
Return Nothing
End Try
End Function
Public Function getUsersLDAP(ByVal strNameSearch As String) As ArrayList
Dim arrProperties(1) As String
Dim strRecherche As String = ""
Dim r As New ArrayList
'initialisation des propriétés à récupérer dans le LDAP
arrProperties(0) = "sn"
arrProperties(1) = "givenName"
'initialisation du filtre de recherche
strRecherche = "(sn=" + strNameSearch + "*)"
'initialisation de la connexion avec l'annuaire et de l'objet de recherche
connection = Me.Connect(_ldapServerName, _login, _motPasse)
Dim resultat As New ArrayList
Dim sb As New StringBuilder(50)
Try
'// creation d'un filtre
Dim SearchRequest As SearchRequest = New SearchRequest()
With SearchRequest
.Scope = Protocols.SearchScope.Subtree
.DistinguishedName = _targetOu
.Filter = strRecherche
End With
Dim searchResponse As SearchResponse = CType(connection.SendRequest(SearchRequest), SearchResponse)
Console.WriteLine("\r\nNombre de réponse:{0}", searchResponse.Entries.Count)
'// creation de la liste de réponse de la recherche
For Each entry As SearchResultEntry In searchResponse.Entries
Console.WriteLine("{0}:{1}", searchResponse.Entries.IndexOf(entry), entry.DistinguishedName)
sb.Remove(0, sb.Length)
For Each attr As String In arrProperties
If Not (entry.Attributes(attr) Is Nothing) Then
sb.Append(entry.Attributes(attr).Item(0))
sb.Append(" ")
End If
Next
resultat.Add(sb.ToString)
Next
Return resultat
Catch ex As Exception
' Console.WriteLine("\nerreur:\n\t{0}: {1}", ex.GetType().Name, ex.Message)
Return Nothing
End Try
End Function |
Partager