|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article describes how to use .NET framework to manage resources under Windows Active Directory Services. Microsoft provides ADSI (Active Directory Services Interface) which can interact with many providers including IIS (Internet Information Services), LDAP (Lightweight Directory Access Protocol), WinNT and NDS (Novell Netware Directory Service). I have also used LDAP in this demo since I thought that it will be useful to know this protocol as it is a platform independent protocol. There are different ways to query the Active Directory Services in a C# program
.NET System.DirectoryServices namespaceThe LDAP format filter stringsThe The OLAP conditional statements are formed by using Active Directory attributes like name, Using the codeThe demo project demonstrates how to query the Active Directory Services and fetch different objects. The LDAP queries and the usage of .NET classes used are confined to The following code in DirectorySearcher ds = new DirectorySearcher();
ds.SearchRoot = new DirectoryEntry("");
// start searching from local domain
ds.Filter = GetFilterString();
// get the LDAP filter string based on selections on the form
ds.PropertyNamesOnly = true;
// this will get names of only those
// properties to which a value is set
ds.PropertiesToLoad.Add("name");
// (PageSize) Maximum number of objects
// the server will return per page
// in a paged search. Default is 0, i.e. no paged search
if (ObjsPerPage.Text.Length > 0)
ds.PageSize = Int32.Parse(ObjsPerPage.Text);
// (ServerPageTimeLimit) the amount of time the server
// should observe to search a page of results
// default is -1, i.e. search indefinitely
if (PageTimeLimit.Text.Length > 0)
ds.ServerPageTimeLimit = new TimeSpan((long)(Decimal.Parse(
PageTimeLimit.Text) * TimeSpan.TicksPerSecond));
// (SizeLimit) maximum number of objects the server
// returns in a search
// default is 0 - interpreted as server
// set default limit of 1000 entries
if (ObjsToFetch.Text.Length > 0)
ds.SizeLimit = Int32.Parse(ObjsToFetch.Text);
// (ServerTimeLimit) amount of time that the server
// should observe in a search
// default is -1 interpreted as server default limit of 120 seconds
if (TotalTimeLimit.Text.Length > 0)
ds.ServerTimeLimit = new TimeSpan((long)(Decimal.Parse(
TotalTimeLimit.Text) * TimeSpan.TicksPerSecond));
// (SearchScope) option to search one level or complete subtree
// default is Subtree, so set this option only if oneLevel is selected
if (searchOptionCB.SelectedIndex == 1)
ds.SearchScope = SearchScope.OneLevel;
// (CacheResults) property by default is true
ds.CacheResults = CacheResultsCB.Checked;
ds.ReferralChasing = ReferralChasingOption.None;
if (SortResultsCB.Checked)
ds.Sort = new SortOption("name", SortDirection.Ascending);
The // form a filter string for the search in LDAP format
private string FormFilter(string objectCategory, string filter)
{
String result;
result = String.Format("(&(objectCategory={0})(name={1}))",
objectCategory, filter);
return result;
}
// this function forms the filter string based on the selected
// objects on the form
private string GetFilterString()
{
// form the filter string for directory search
string filter = "";
if (UsersCB.Checked)
filter += FormFilter("user", UsersFilter.Text);
if (ComputersCB.Checked)
filter += FormFilter("computer", ComputersFilter.Text);
if (PrintersCB.Checked)
filter += FormFilter("printQueue", PrintersFilter.Text);
// add all the above filter strings
return "(|" + filter + ")";
}
Important Points and Notes
|
||||||||||||||||||||||