Active Directory Authentication



Download source code by Clicking here

Authenticate users accessing application by using Microsoft Active Directory using Microsoft DirectoryServices.


Security is one of the most important thing while developing an application for an Enterprise. Active Directory is a directory service used to store information about the network resources across a domain and also centralize the network. Using Active directory authentication you can allow users under a domain to access application along with the roles as built in active directory.


Using the code

To use Active Directory authentication DirectoryEntry and DirectorySearcher classes will be used, these classes are part of Microsoft DirectoryServices.

First Add reference to .Net Interop System.DirectoryServices then create instance of DirectoryEntry and DirectorySearcher Class.

   1: private DirectoryEntry entry; 
   2: entry = new DirectoryEntry("LDAP://" + oDomainName + "", oUserName, 
   3:         oPassword, System.DirectoryServices.AuthenticationTypes.Secure);
   5: private DirectorySearcher ds;
   6: entry = new DirectoryEntry("LDAP://" + oDomainName + "", oUserName, 
   7:         oPassword, System.DirectoryServices.AuthenticationTypes.Secure);

Then made a search in active directory against your login credentials, if searchresult returns object of the user it means that user exist in the Active Directory with the submitted credentials.

The source code also contains a method "GetDomains" which returns an ArrayList having all the domains that are present in the network.



   1:  public ArrayList GetDomains()
   2:          {
   3:              ArrayList arrDomains = new ArrayList();
   4:              DirectoryEntry ParentEntry = new DirectoryEntry();
   5:              try
   6:              {
   7:                  ParentEntry.Path = "WinNT:";
   8:                  foreach (DirectoryEntry childEntry in ParentEntry.Children)
   9:                  {
  10:                      switch (childEntry.SchemaClassName)
  11:                      {
  12:                          case "Domain":
  13:                              {
  14:                                  arrDomains.Add(childEntry.Name);
  15:                                  break;
  16:                              }
  17:                          default:
  18:                              {
  19:                                  break;
  20:                              }
  21:                      }} }
  22:             catch (Exception e)
  23:              {
  24:              }
  25:              finally
  26:              {
  27:                  ParentEntry = null;
  28:              }
  29:             return arrDomains;
  30:         } 


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s