Programmatically Authenticating Users Using ADAM

by Ray 3. July 2010 13:50

Recently I was attempting to migrate an application from Java to .NET that leveraged ADAM as it’s user store. On first look you think, “No problem, .NET comes with an Active Directory Provider, this should be easy.” For me this wasn’t the case, a slew of problems eventually had me looking at a way to do the authentication myself.  After doing some Binging I finally came across a very interesting site that was completly dedicated to programming .NET applications with Active Directory here<

Here is the process that I went through to do this, this is assuming that you have ADAM already installed and configured on a machine somewhere.

.NET Comes with two namespaces that are essential when working with Active Directory, System.DirectoryServices which will be referred to as SDS and System.DirectoryServices.Protocols which will be referred to as SDS.P.

In order to streamline the process I created an Authentication class that I could call throughout my application:

 /// <summary> 
     /// Contains methods used to authenticate users 
     /// </summary> 
     public  class  Authentication 
     {
         /// <summary> 
         /// Gets or sets the path. 
         /// </summary> 
         /// <value>The path.</value> 
         public  string  Path { get ; set ; }
         /// <summary> 
         /// Gets or sets the filter attribute. 
         /// </summary> 
         /// <value>The filter attribute.</value> 
         public  string  FilterAttribute { get ; set ; }
 
         /// <summary> 
         /// Initializes a new instance of the <see cref="Authentication"/> class. 
         /// </summary> 
         /// <param name="path">The path.</param> 
         public  Authentication(string  path)
         {
             Path = path;
         }
 
         /// <summary> 
         /// Authenticates the user. 
         /// </summary> 
         /// <param name="username">The username of the user you are trying to authenticate.</param> 
         /// <param name="password">The password of the user you are trying to authenticate.</param> 
         /// <returns>true if authenticated otherwise dalse</returns> 
         public  bool  AuthenticateUser(string  username, string  password)
         {
             //Create a connection to the ldap server you want to authenticate against 
             LdapConnection  connection = new  LdapConnection (Path);
 
             //These options do not need to be set and are specific to the environment 
             var  ldapSessionOptions = connection.SessionOptions;
             ldapSessionOptions.ProtocolVersion = 3;
             ldapSessionOptions.SecureSocketLayer = true ;
 
             connection.AuthType = AuthType .Basic;
 
             //Set our connection credentials to our supplied username and password. 
             NetworkCredential  credential = new  NetworkCredential (username, password);
             connection.Credential = credential;
 
             try 
             {
                 //Check if the credentials are valid.  If they are not, the ldap connection will not bind. 
                 connection.Bind();
             }
             catch (Exception  ex)
             {
                 throw  new  Exception (ex.ToString());
             }
 
             return  true ;
         }
     }
 

Now when you want to authenticate, you just instantiate the class and then call the AuthenticateUser method supplying the username and password.