Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Active Directory Tool

4.60/5 (5 votes)
25 Apr 2009CPOL2 min read 46K   2.5K  
Active directory tool - helps manage active directory oparations (query, update and delete) with easy to use GUI

Introduction

The goal of the application was to build a users management assistant application for any organization working with Microsoft Active Directory Services:

  • IT Managers – Get self customized reports and Information in a better friendly way
  • IT Workers – Verify data inserted in users and groups objects, make updates to the active directory and get self customized reports

Background

It is part of college final project.

Prerequisites

  • Framework 3.5 installed on the computer (can be downloaded from Microsoft web site)
  • Connection to Microsoft Active Directory
  • Microsoft Office Excel installed (97 and up)
  • Microsoft SQL server 2005 installed in the organization
  • Can also be done by "ini" files instead of DB
  • Microsoft patterns & practices Version=4.1.0.0 is needed to be installed in dev computer

Using the Application

Step 1 Install the DB (MSSQL)

Open Microsoft-SQL 2005 and restore database from EmptyDB.bak (which is in the zip file).

Step 2 Run the Program

  • Compile and run the program (DirectoryGate project)
  • Choose the DB server which you installed the DB in.
  • Click on the "Add - Edit Domain List" button and set the Domain
  • Start to query/update/delete your Active Directory
  • The results are in Excel file in your application folder

About the Code

The solution is separated into 6 projects:

  1. ActiveDirectoryDO - static class handles all the active directory operations
  2. DirectoryGate - The UI for the application includes all the forms and UI of the application
  3. DirectoryGateDO - static class for handling the database layer - using Microsoft patterns & practices for best practice approaches
  4. HandleError - static class handles all the error messages in the application
  5. OutputToExcel - for outputting the results to Excel
  6. OutputToHTML - for outputting the results to HTML

ActiveDirectoryDO

CreateDirectoryEntry - Returns the DirectoryEntry by given Domain.

It is used in all update/query/delete Active directory methods.

C#
public static DirectoryEntry CreateDirectoryEntry(string domainADPath)
        {
            // create and return new LDAP connection with desired settings
            DirectoryEntry LDAPConnection = 
			new DirectoryEntry(domainADPath); //litwareinc.com

            string[] DomainSplit = domainADPath.Split('.');

            //LDAPConnection.Path = "LDAP://DC=litwareinc,DC=com";
            LDAPConnection.Path = "LDAP://";

            foreach (string Part in DomainSplit)
            {
                LDAPConnection.Path += "DC=" + Part + ",";
            }

            int LastCommaInd = LDAPConnection.Path.LastIndexOf(',');

            LDAPConnection.Path = LDAPConnection.Path.Remove(LastCommaInd);

            LDAPConnection.AuthenticationType = AuthenticationTypes.Secure;

            return LDAPConnection;
        }

Example for Active directory query method is as follows:

C#
public static void SearchObjectsInActiveDirectory(string domainADPath,
    string searchString, ref List<string> deletionObjList)
        {
            // create LDAP connection object
            using(DirectoryEntry LDAPConnection = CreateDirectoryEntry(domainADPath)){

            	using(DirectorySearcher SearchObj = 
				new DirectorySearcher(LDAPConnection)){

            		// Groups + Users Search
            		SearchObj.Filter = searchString;

            		SearchResultCollection Result = SearchObj.FindAll();

            		foreach (SearchResult SR in Result)
            		{
                			ResultPropertyCollection Fields = SR.Properties;

                			foreach (Object myCollection in Fields
							["samAccountName"])
                			{
                    			deletionObjList.Add
						(myCollection.ToString());
                			}
            		}
	   	}
	   }
        }

DirectoryGateDO  (Using Microsoft Patterns & Practices)

Example for SQL StoredProcedure used for retrieving all the Domains within the database:

C#
   public static SqlDataReader GetDomainsFromDB()
{ 
    SqlDatabase db = new SqlDatabase(m_CStr);
        // 1.  create a command object identifying
       //     the stored procedure
        DbCommand SComm = db.GetStoredProcCommand("spGetDomainsList");
        // execute the command
        SqlDataReader SDR = (SqlDataReader)db.ExecuteReader(SComm);
        return SDR;
    }

Points of Interest

Please come with suggestions and ideas of how to improve the application.

History

  • Alpha version of the project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)