LDAP sous Mono

05 Jul 2007

using System;
using System.Security.Cryptography;
using System.DirectoryServices;
using Novell.Directory.Ldap;
using System.Security.Cryptography.X509Certificates;

namespace MyProjet.Test {

public class LDAP {

    public static void Authentificate(string login, byte[] passwd) {

    // parametres de connection
    string ldapHost = "host.du.ldap.fr";
    int ldapPort = 636;

    // on lance la connection SSL
    LdapConnection conn = new LdapConnection();
    conn.SecureSocketLayer = true;
    conn.UserDefinedServerCertValidationDelegate += new CertificateValidationCallback(SSLHandler);
    conn.Connect (ldapHost, ldapPort);
    conn.Bind (null, null);

    // parametres de recherche
    string baseDN = "dc=wp,dc=paris,dc=fr";
    string filter = "(uid=" + login + ")";
    string[] attributes = {"group","corps"};

    // on lance la recherche
    LdapSearchResults results = conn.Search(baseDN, LdapConnection.SCOPE_SUB, filter, attributes, false);
    LdapEntry result = results.next();

    // on recupere les champs
    string group = result.getAttribute("group").StringValueArray[0];

    // on ferme la connection
    conn.Disconnect();

    [...]

    }

    //pour la connection SLL
    public static bool SSLHandler (X509Certificate certificate, int[] certificateErrors) {
    return true;
    }
}
}