When you are developing an application that uses active directory users, without using the default membership provider, you'll need to write some default login logic. In my application a perform a login check and when users are added to the application, I perform a check if the users exists.

First the login check:

public bool IsAuthenticated(string domain, string username, string pwd)    
{      
  string domainAndUsername = domain + @"\" + username;      string path = "";      
  DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pwd);      
  try      
  {          
    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + username + ")";          
    search.PropertiesToLoad.Add("cn");          
    SearchResult result = search.FindOne();     if (null == result)          
    {
              return false;          
    }      
  }      
  catch (Exception ex)      
  {          throw ex;      
  }    return true;    
} 

 

This function simply checks if a user with password and login name exists in AD. We use username and password to create the directory entry and perform an action with this entry. If the username or the password is invalid, we get this error: Logon failure: unknown user name or bad password. 

 

The next AD action is to lookup if the user exists:

  DirectoryEntry de = new DirectoryEntry();        
  de.Path = @"ldap://OU=Employees/, OU=Users, OU=Location, OU=INT-ORGANISATION,   
  OU=UNIT, OU=Users & Workstations, DC=comp,DC=net"; 
  DirectorySearcher dSearch = new DirectorySearcher();   dSearch.SearchRoot = de;        
  dSearch.Filter = "(&(objectClass=user) (samaccountname=" + txtUser.Text + "))";   
  SearchResult result = dSearch.FindOne();        
  if (result != null)        
  {
    errName.HideError();            
    return true;
   }
   else
   {
     return false;        
   } 

 

Here's an example of active directory properties with dummy values!