What is LDAP (Lightweight Directory Access Protocol) and how to use LDAP it in Asp.Net - Part 1

This article provides you a small introduction about LDAP - Lightweight Directory Access Protocol - I have explained very basic info related to LDAP and provided a simple example how to use it in Asp.net. The example will display information about the given user

What is LDAP?

LDAP(Lightweight Directory Access Protocol) is an internet/application protocol for accessing information about users in a directory.

A directory can contain different objects like users or groups or printers or computers or scanners etc. in a specific domain related to a company.

LDAP is used by the client programs (outlook or lotus-notes or any other emailing service) to communicate with the servers. LDAP not only used for Client to server communication but also servers to servers communication .

LDAP is mostly used to search & locate for a specific object or specific user or a specific group in a domain.

The following are some of attributes that are used while searching for any object using LDAP

ObjectCategory : It can be User or person or Computer Name or group name
sAMAccountName : It will give logon user id (ie his/her network id)
SN : It will give last name or surname.
givenName : It will give Firstname.
cn : It will also give network id or Logon user id
mail : It will give mail id of the logon user
company : It will give Company name / organization name
department : It will give you department name
manager : It will give manager network id of the logon user
streetAddress : It will give street address
postalCode : It will give postal code or zip code

like this many attributes are there.

How to Use LDAP in Asp.net
In Asp.Net, you need to include the references of 'Directory Services' for using LDAP in your application

To add Directory Services reference to your application
1. Open Solution Explorer and then goto 'References'
2. Right click on the 'References' and choose 'Add Reference' option
3. Now it displays, 'Add Reference' dialog window. Here goto .Net tab
4. Now search for 'System.DirectoryServices' dll, once you find it just select it and click on 'Ok' button
5. Now the reference is added to your project

Directory services : It is a DLL, which is having methods to access/get information related to an employee/person details (first/last name, full name, address, phone, email etc), group, domain etc.

Once you have added the reference of 'Directory Services' to your project, then try the below example

Example: To get user information: In your organization, you know only his network id and you want to know his other details like full name, company name, department name, email id etc

Aspx Desing
Add a textbox and set its TextMode property to multi-line

Code Behind Logic

using System;
using System.DirectoryServices;

protected void Page_Load(object sender, EventArgs e)
TextBox1.Text = GetLoggedInUserName("GiveTheNetworkId");

private string GetLoggedInUserName(string strLoggedUser)
string strUserName = string.Empty;
StringBuilder sb = new StringBuilder();
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(sAMAccountName={0})", strLoggedUser);

result = search.FindOne();

if (result != null)
if (result.Properties["cn"].Count > 0)
sb.Append("cn :" + (string)result.Properties["cn"][0]).Append(Environment.NewLine);
if (result.Properties["distinguishedName"].Count > 0)
sb.Append("distinguishedName :" + (string)result.Properties["distinguishedName"][0]).Append(Environment.NewLine);
if (result.Properties["givenname"].Count > 0)
sb.Append("givenname :" + (string)result.Properties["givenname"][0]).Append(Environment.NewLine);
if (result.Properties["samaccountname"].Count > 0)
sb.Append("samaccountname :" + (string)result.Properties["samaccountname"][0]).Append(Environment.NewLine);
if (result.Properties["sn"].Count > 0)
sb.Append("sn :" + (string)result.Properties["sn"][0]).Append(Environment.NewLine);
if (result.Properties["mail"].Count > 0)
sb.Append("mail :" + (string)result.Properties["mail"][0]).Append(Environment.NewLine);
if (result.Properties["company"].Count > 0)
sb.Append("company :" + (string)result.Properties["company"][0]).Append(Environment.NewLine);
if (result.Properties["department"].Count > 0)
sb.Append("department :" + (string)result.Properties["department"][0]).Append(Environment.NewLine);
return sb.ToString();

Get User Info Output Screen shot using LDAP

For More information, please refer the following links

Part 2 will be coming soon : In part 2, we can see few more examples like how to get groups that are available in LDAP, how to get users list under a group, how to get nested groups etc



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: