Tuesday, February 20, 2018

How to get User Data from the Active Directory

Introduction

Last month the project manager asked me write to find all users information from the Active directory and which all fields are missing information for particular user. I was trying to search in Internet for information about .NET Active Directory examples, I could not able to find much information on the net, it prompted me write an article on this topic.
In this article, I will explain how to use Active Directory class and retrieve data from the component classes. You can cut and past below code lines and execute it but you need to pass domain name in Directory Entry constructor. Following example taken from one of my developed projects and modified for easy to understand.
I assumed that you have a general understanding of active directory before using this example.

Step 1:

Add System.DirectoryServices.Dll (from Project Add reference)
System.DirectoryServices provides easy access to active directory from managed code. This namespace contains two components classes, DirectoryEntry and DirectorySearcher.

Step 2:

Using System.DirectoryServices
Directory Entry Class: this class encapsulates a node or object in the active directory hierarchy. Use this class for binding to objects, reading properties and updating attributes.

Step 3:

DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");
Directory Searcher: It will perform queries against the active directory hierarchy

Step 4:

DirectorySearcher Dsearch = new DirectorySearcher(entry);

Step 5:

String Name="Richmond";
The Filter property supports for the all filter the information of the active directory.
// l = city name

Step 6:

dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
Executes the search and returns a collection of the entries that are found.

Step 7:

This function checks active directory field is valid or not. Add this member function to you class.
Public static string GetProperty(SearchResult searchResult, 
 string PropertyName)
  {
   if(searchResult.Properties.Contains(PropertyName))
   {
    return searchResult.Properties[PropertyName][0].ToString() ;
   }
   else
   {
    return string.Empty;
   }
  }

Step 8:

// get all entries from the active directory.
// Last Name, name, initial, homepostaladdress, title, company etc..
 foreach(SearchResult sResultSet in dSearch.FindAll())
    {
          
     // Login Name
     Console.WriteLine(GetProperty(sResultSet,"cn"));
     // First Name
     Console.WriteLine(GetProperty(sResultSet,"givenName"));
     // Middle Initials
     Console.Write(GetProperty(sResultSet,"initials"));
     // Last Name
     Console.Write(GetProperty(sResultSet,"sn"));
     // Address
 string tempAddress=GetProperty(sResultSet,"homePostalAddress");

     if(tempAddress !=string.Empty)
     {
          string[] addressArray = tempAddress.Split(';');
      string taddr1,taddr2;
      taddr1=addressArray[0];
      Console.Write(taddr1);
      taddr2=addressArray[1];
      Console.Write(taddr2);
     }
     // title
     Console.Write(GetProperty(sResultSet,"title"));
     // company
     Console.Write(GetProperty(sResultSet,"company"));
     //state
     Console.Write(GetProperty(sResultSet,"st"));
     //city
     Console.Write(GetProperty(sResultSet,"l"));
     //country
     Console.Write(GetProperty(sResultSet,"co"));
     //postal code
     Console.Write(GetProperty(sResultSet,"postalCode"));
     // telephonenumber
     Console.Write(GetProperty(sResultSet,"telephoneNumber"));
     //extention
     Console.Write(GetProperty(sResultSet,"otherTelephone"));
    //fax
     Console.Write(GetProperty(sResultSet,"facsimileTelephoneNumber"));
 
    // email address
     Console.Write(GetProperty(sResultSet,"mail"));
     // Challenge Question
     Console.Write(GetProperty(sResultSet,"extensionAttribute1"));
     // Challenge Response
     Console.Write(GetProperty(sResultSet,"extensionAttribute2"));
     //Member Company
     Console.Write(GetProperty(sResultSet,"extensionAttribute3"));
     // Company Relation ship Exits
     Console.Write(GetProperty(sResultSet,"extensionAttribute4"));
     //status
     Console.Write(GetProperty(sResultSet,"extensionAttribute5"));
          // Assigned Sales Person
     Console.Write(GetProperty(sResultSet,"extensionAttribute6"));
     // Accept T and C
     Console.Write(GetProperty(sResultSet,"extensionAttribute7"));
     // jobs
     Console.Write(GetProperty(sResultSet,"extensionAttribute8"));
   String tEamil = GetProperty(sResultSet,"extensionAttribute9");
 
     // email over night
   if(tEamil!=string.Empty)
   {
      string em1,em2,em3;
    string[] emailArray = tEmail.Split(';');
      em1=emailArray[0];
      em2=emailArray[1];
      em3=emailArray[2];
      Console.Write(em1+em2+em3);
      
   }
     // email daily emerging market
     Console.Write(GetProperty(sResultSet,"extensionAttribute10"));
     // email daily corporate market
   Console.Write(GetProperty(sResultSet,"extensionAttribute11"));
     // AssetMgt Range
     Console.Write(GetProperty(sResultSet,"extensionAttribute12"));
     // date of account created
     Console.Write(GetProperty(sResultSet,"whenCreated"));
   // date of account changed
     Console.Write(GetProperty(sResultSet,"whenChanged"));
}
Enjoy.

source : https://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory

No comments:

Post a Comment