I was reading an article about Active Directory as Active directory is installed in our office recenly. It was really nice article and the auther has mentioned that you can access active directory using c# or any language. I tried to implement that, obviously I had search on google and found some cod for same. I chose the one and modify it according to my need. Below is the code,
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.DirectoryServices;namespace DirectorySearcher_infovish
{
/// <summary>
/// Summary description for DirectorySearcher.
/// </summary>
public class DirectorySearch
{
public Hashtable objHash;
static int count=0;
SortedList objSortedList;
public string domain;public DirectorySearch()
{
}public void FillEmployee(System.Web.UI.WebControls.DropDownList ddlEmpName)
{
string domain = System.Configuration.ConfigurationSettings.AppSettings["DomainIP"];
DirectoryEntry de = new DirectoryEntry("LDAP://"+domain);
de.Username = username; // Domain Admin user name
de.Password = password; // Domain Admin password
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=user)(objectCategory=person))";
ds.Sort.PropertyName = "CN"
ds.Sort.Direction = SortDirection.Ascending;
ds.SearchScope = SearchScope.Subtree;
ds.PageSize = 4000;count++;
try
{
objSortedList = new SortedList();
foreach(SearchResult result in ds.FindAll())
{
DirectoryEntry deTemp = result.GetDirectoryEntry();
try
{
objSortedList.Add (deTemp.Properties["cn"].Value.ToString(),deTemp.Properties["Mail"].Value.ToString());
}
catch(Exception ex)
{
ex.Message.ToString();
}
}
FillGroup(ddlEmpName);
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.ToString());
}
finally
{
}
}}
}
Happy Programming.
No comments:
Post a Comment