title: c# Log In click event using authentication against Active Directory through LDAP
body: <p>I am quite new to c# and LDAP, I'm doing this project so that I could learn about them in a more hands on approach.</p>
  
  <p>What I'm trying to create is a Log in form that has a log in click event that would authenticate the username and password after the user enters them through the active directory using LDAP. </p>
  
  <p>I have read <a href="http://msdn.microsoft.com/en-us/magazine/cc135979.aspx" rel="nofollow noreferrer">Managing Directory Security Principals in the .NET Framework 3.5</a> to be able to understand this subject better and I have also gone through similar topics here this one dealing with the validation in itself (<a href="https://stackoverflow.com/questions/290548/validate-a-username-and-password-against-active-directory">c# - Validate a username and password against Active Directory?</a>) and this one authenticating a username (<a href="https://stackoverflow.com/questions/469588/c-sharp-against-active-directory-over-ldap">c# against Active Directory over LDAP</a>) </p>
  
  <p>From the first linked topic I had learned that the following code should do the trick in authenticating a username and password:</p>
  
  <pre><code>  using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.string.com/OU=Users, Dc=example, Dc= string, DC=com"))
          {
              bool isValid = pc.ValidateCredentials(User, Password);
          }
  </code></pre>
  
  <p>So my approach to incorporate this to a click event was as follows:</p>
  
  <pre><code>using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.string.com/OU=Users, Dc=example, Dc= string, DC=com"))
  
              bool isValid = pc.ValidateCredentials(User, Password);
  
               if(isValid)
               {
                   Main m = new Main();
                   this.Close();
                   m.Show();
               }
               else
               {
                   MessageBox.Show("Invalid Username and/or Password","Error!");
                   textBox1.Clear();
                   textBox2.Clear();
                   textBox1.Focus();
  
               }
  </code></pre>
  
  <p>Which is giving me a bool error of Embedded Statement.
  I tried the other approach I had read from the second post which was to use this code which authenticates only Username: </p>
  
  <pre><code>         PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.com/OU=Computers,OU=Users,dc=example,dc=com");
  
          UserPrincipal user = UserPrincipal.FindByIdentity(pc, "username");
  
  
          bool userExists = (user != null);
  </code></pre>
  
  <p>But I found that I wont be able to authenticate a password using this method as <strong>UserPrincipal.FindByPassword</strong> does not exist.</p>
  
  <p>I have also tried it this way but again <strong>.Password</strong> does not exist:</p>
  
  <pre><code> PrincipalContext pc = new PrincipalContext(ContextType.Domain,"LDAP://....");
  
          UserPrincipal qbeUser = new UserPrincipal(pc);
          qbeUser.EmployeeId = User;
  
          //.Password does not exist
          UserPrincipal qbePassword = new UserPrincipal(pc);
          qbePassword.Password = Password;
  
          // create your principal searcher passing in the QBE principal    
          PrincipalSearcher srchUser = new PrincipalSearcher(qbeUser);
           PrincipalSearcher srchPass = new PrincipalSearcher(qbePassword);
          // try to find that user and password
          UserPrincipal founduser = srchUser.FindOne() as UserPrincipal;
          UserPrincipal foundpass = srchPass.FindOne() as UserPrincipal;
  
          if (founduser != null)
          {
              if (foundpass != null)
              {
                  Main m = new Main();
                  this.Close();
                  m.Show();
              }
              else
              {
                  MessageBox.Show("Password Not Valid.");
                  textBox2.Clear();
                  textBox2.Focus();
              }
          }
  
          else
          {
              MessageBox.Show("Username Not Valid.");
              textBox1.Clear();
              textBox1.Focus();
          }
  </code></pre>
  
  <p>Can someone kindly please instruct me as how one should correctly approach this.</p>
  
  <p>Thank you in advance.</p>