C# LDAP Wrapper
Before you try to run the following code, please download Novel LDAP lib:
http://forge.novell.com/modules/xfcontent/downloads.php/ldapcsharp/ldapcsharp/CsharpLDAP-v2.1.10/
using System;
using System.Collections.Generic;
using System.Text;
using Novell.Directory.Ldap;
namespace LDAPUtility
{
class LDAPUtil
{
private string ldapHost = "ssuzdc3";
private int ldapPort = 389;
LdapConnection ldapConn = null;
private void Connect()
{
try
{
// Creating an LdapConnection instance
ldapConn = new LdapConnection();
// Connect function will create a socket connection to the server
ldapConn.Connect(ldapHost, ldapPort);
// Bind function with null user dn and password value will perform anonymous bind
// to LDAP server
ldapConn.Bind(null, null);
}
catch (Exception e)
{
// failed to connect to server
}
}
private void Disconnect()
{
ldapConn.Disconnect();
ldapConn = null;
}
public string GetSupervisor(string id)
{
Connect();
string boss = "";
try
{
LdapSearchResults lsc = ldapConn.Search("OU=Users,OU=Suzhou,DC=charry,DC=org",
LdapConnection.SCOPE_ONE,
"sAMAccountName=" + id,
null,
false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
// Exception is thrown, go for next entry
continue;
}
LdapAttribute attribute = nextEntry.getAttribute("manager");
boss = attribute.StringValue;
}
}
catch (Exception e)
{
// exception
}
Disconnect();
return GetAMAcountName(boss);
}
private string GetFullName(string id)
{
// CN=Wang\, Charry,OU=Users,OU=Suzhou,DC=charry,DC=org
int end = id.IndexOf("OU=");
id = id.Substring(3, end - 4);
id = id.Replace("\\", "");
160; return id;
}
// convert distinguished name to AMAcountName
public string GetAMAcountName(string id)
{
id = GetFullName(id);
Connect();
string tmp = "";
try
{
LdapSearchResults lsc = ldapConn.Search("OU=Users,OU=Suzhou,DC=charry,DC=org",
LdapConnection.SCOPE_ONE,
"displayName=" + id,
null,
false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
// Exception is thrown, go for next entry
continue;
}
LdapAttribute attribute = nextEntry.getAttribute("sAMAccountName");
tmp = attribute.StringValue;
}
}
catch (Exception e)
{
// exception
}
Disconnect();
return tmp;
}
public string GetDisplayName(string id)
{
Connect();
string name = "";
// get fullname
try
{
LdapSearchResults lsc = ldapConn.Search("OU=Users,OU=Suzhou,DC=charry,DC=org",
LdapConnection.SCOPE_ONE,
"sAMAccountName=" + id,
null,
false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
// Exception is thrown, go for next entry
continue;
}
0; LdapAttribute attribute = nextEntry.getAttribute("displayName");
name = attribute.StringValue;
}
}
catch (Exception e)
{
// exception
}
Disconnect();
return name;
}
public string GetEmail(string id)
{
Connect();
string email = "";
try
{
LdapSearchResults lsc = ldapConn.Search("OU=Users,OU=Suzhou,DC=charry,DC=org",
LdapConnection.SCOPE_ONE,
"sAMAccountName=" + id,
null,
false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
// Exception is thrown, go for next entry
continue;
}
LdapAttribute attribute = nextEntry.getAttribute("mail");
email = attribute.StringValue;
}
}
catch (Exception e)
{
// exception
}
Disconnect();
return email;
}
public static void test()
{
string ldapHost = "ssuzdc3";
int ldapPort = 389;
try
{
// Creating an LdapConnection instance
LdapConnection ldapConn = new LdapConnection();
// Connect function will create a socket connection to the server
ldapConn.Connect(ldapHost, ldapPort);
// Bind function with null user dn and password value will perform anonymous bind
// to LDAP server
ldapConn.Bind(null, null);
// Searches in the Marketing container and return all child entries just below this
// container i.e. Single level search
LdapSearchResults lsc = ldapConn.Search("OU=Users,OU=Suzhou,DC=charry,DC=org",
LdapConnection.SCOPE_ONE,
"sAMAccountName=qinick",
null
,
false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
Console.WriteLine("Error: " + e.LdapErrorMessage);
// Exception is thrown, go for next entry
continue;
}
Console.WriteLine("\n" + nextEntry.DN);
LdapAttributeSet attributeSet = nextEntry.getAttributeSet();
System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
while (ienum.MoveNext())
{
LdapAttribute attribute = (LdapAttribute)ienum.Current;
string attributeName = attribute.Name;
string attributeVal = attribute.StringValue;
Console.WriteLine(attributeName + "value:" + attributeVal);
}
}
ldapConn.Disconnect();
}
catch (Exception e)
{
string x = e.Message;
}
Console.Read();
}
}
}