Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to authenticate user in Ldap / OpenLdap using C# (user: test in domain, server on ip port 389)
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Security.Permissions;
namespace LdapConnection
[DirectoryServicesPermission(SecurityAction.LinkDemand, Unrestricted = true)]
public class LdapConnect
public static void Main(string[] args)
// Create the new LDAP connection
LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier("", 389);
System.DirectoryServices.Protocols.LdapConnection ldapConnection =
new System.DirectoryServices.Protocols.LdapConnection(ldi);
Console.WriteLine("LdapConnection is created successfully.");
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.SessionOptions.ProtocolVersion = 3;
NetworkCredential nc = new NetworkCredential("uid=testa,ou=people,dc=ghashd,dc=servebeer,dc=com",
"zaq12wsx"); //password
Console.WriteLine("LdapConnection authentication success");
catch (LdapException e)
Console.WriteLine("\r\nUnable to login:\r\n\t" + e.Message);
catch (Exception e)
Console.WriteLine("\r\nUnexpected exception occured:\r\n\t" + e.GetType() + ":" + e.Message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.