Friday, April 15, 2011

Access Active Directory in .NET

During one of my client requirements, I played a lot with active directory to display user data in a web application from active directory.

Here goes a generic class with some functions to get get user information from active directory:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.DirectoryServices;
using System.Collections;

namespace Common
{
public class ActiveDirectory
{
string domainPath = "GC://YourGC/DC=YourDomain,dc=YourOU,dc=com";

//Get Single user info
public DataTable GetUserInfo(string sLogonName)
{
/*Getting user data from active directory*/
DirectoryEntry entry = new DirectoryEntry(domainPath);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + sLogonName + "))";
SearchResult result = searcher.FindOne();

/*Creating a datatable to store user data*/
DataTable resultTable = new DataTable("UserData");
resultTable.Columns.Add("givenname");
resultTable.Columns.Add("sn");
resultTable.Columns.Add("title");
resultTable.Columns.Add("telephoneNumber");
resultTable.Columns.Add("company");
resultTable.Columns.Add("mail");
resultTable.Columns.Add("department");
resultTable.Columns.Add("manager");

DataRow resultRow = resultTable.NewRow();

if (result.Properties.Count > 0)
{
try
{
resultRow["givenname"] = result.Properties["givenname"][0].ToString();
}
catch (Exception)
{
resultRow["givenname"] = string.Empty;
}
try
{
resultRow["sn"] = result.Properties["sn"][0].ToString();
}
catch (Exception)
{
resultRow["sn"] = string.Empty;
}
try
{
resultRow["title"] = result.Properties["title"][0].ToString();
}
catch (Exception)
{
resultRow["title"] = string.Empty;
}
try
{
resultRow["telephoneNumber"] = result.Properties["telephoneNumber"][0].ToString();
}
catch (Exception)
{
resultRow["telephoneNumber"] = string.Empty;
}
try
{
resultRow["company"] = result.Properties["company"][0].ToString();
}
catch (Exception)
{
resultRow["company"] = string.Empty;
}
try
{
resultRow["mail"] = result.Properties["mail"][0].ToString();
}
catch (Exception)
{
resultRow["mail"] = string.Empty;
}
try
{
resultRow["department"] = result.Properties["department"][0].ToString();
}
catch (Exception)
{
resultRow["department"] = string.Empty;
}
try
{
resultRow["manager"] = result.Properties["manager"][0].ToString();
}
catch (Exception)
{
resultRow["manager"] = string.Empty;
}
resultTable.Rows.Add(resultRow);
}
else
{

}
return resultTable;
}

//Get all domain users multiple properties in data table
public DataTable GetAllUserInfo()
{
/*Getting user data from active directory*/
DirectoryEntry entry = new DirectoryEntry(domainPath);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.PageSize = 1000;

searcher.Filter = "(&(objectClass=user)(objectCategory=person))"; //to get users only
SearchResultCollection resultCollection = searcher.FindAll();

/*Creating a datatable to store user data*/
DataTable resultTable = new DataTable("UserData");
resultTable.Columns.Add("givenname");
resultTable.Columns.Add("sn");
resultTable.Columns.Add("title");
resultTable.Columns.Add("telephoneNumber");
resultTable.Columns.Add("company");
resultTable.Columns.Add("mail");
resultTable.Columns.Add("department");

SearchResult result;
if (resultCollection != null)
{
for (int counter = 0; counter < resultCollection.Count; counter++)
{
DataRow resultRow = resultTable.NewRow();

result = resultCollection[counter];
if (result.Properties.Contains("givenname"))
{
resultRow["givenname"] = result.Properties["givenname"][0].ToString();
}
if (result.Properties.Contains("sn"))
{
resultRow["sn"] = result.Properties["sn"][0].ToString();
}
if (result.Properties.Contains("title"))
{
resultRow["title"] = result.Properties["title"][0].ToString();
}
if (result.Properties.Contains("telephoneNumber"))
{
resultRow["telephoneNumber"] = result.Properties["telephoneNumber"][0].ToString();
}
if (result.Properties.Contains("company"))
{
resultRow["company"] = result.Properties["company"][0].ToString();
}
if (result.Properties.Contains("mail"))
{
resultRow["mail"] = result.Properties["mail"][0].ToString();
}
if (result.Properties.Contains("department"))
{
resultRow["department"] = result.Properties["department"][0].ToString();
}
resultTable.Rows.Add(resultRow);
}
}
return resultTable;
}


//Get all domain users single property in array list
public ArrayList GetAllADDomainUsers()
{
ArrayList allUsers = new ArrayList();

DirectoryEntry searchRoot = new DirectoryEntry(domainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";

SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("samaccountname"))
{
allUsers.Add((String)result.Properties["samaccountname"][0]);
}
}
}
return allUsers;
}
}
}



Happy Coding!!!

No comments:

Post a Comment