Connecting to LDAP with Ldap4Net

Ldap4Net is an open source library for .NET that can work with LDAP repositories: https://github.com/flamencist/ldap4net

Here is an example where we try to authenticate with a simple bind mechanism, using username and password. The username format depends on the LDAP, but for some, it can be sAMAccountName while for others it should be a UPN


using LdapConnection connection = new();

connection.Connect(ldapHost, ldapPort);

_logger.LogDebug("Trying to login with user: {username}", username);

connection.Bind(LdapForNet.Native.Native.LdapAuthType.Negotiate,
    new LdapCredential() { UserName = username, Password = password });
    

To use GSSAPI method, write this instead:

connection.Bind(LdapAuthMechanism.GSSAPI);

Post a Comment

Previous Post Next Post