title: LDAP | Check If UserID is Existing on AD
body: <p>I just want to Add a new method on an existing code below.
  Method is a simply check a given <strong>User_ID</strong> if it is exists on the AD.</p>
  
  <p>It's my 1st time dealing with AD.</p>
  
  <pre><code>public class AD
  {
      // Fields
      private static string ADPassword = ConfigurationManager.AppSettings["ADPassword"].ToString();
      private static string ADPath = ConfigurationManager.AppSettings["ADConnection"].ToString();
      private static string ADServerName = ConfigurationManager.AppSettings["ADServerName"].ToString();
      private static string ADUserName = ConfigurationManager.AppSettings["ADUserName"].ToString();
  
      // Methods
      public static string GetLogin(string sUserName, string sPassword)
      {
          try
          {
              DirectoryEntry entry = new DirectoryEntry(ADPath, ADServerName + sUserName, sPassword);
              object nativeObject = entry.NativeObject;
              return string.Empty;
          }
          catch
          {
              return "Invalid Username or Password";
          }
      }
  
      public static string Update(string sUserName, string sOldPassword, string sNewPassword)
      {
          string message;
          try
          {
              DirectoryEntry searchRoot = new DirectoryEntry();
              searchRoot.Path = ADPath;
              searchRoot.Username = ADServerName + ADUserName;
              searchRoot.Password = ADPassword;
              DirectorySearcher searcher = new DirectorySearcher(searchRoot);
              searcher.Filter = "(SAMAccountName=" + sUserName + ")";
              DirectoryEntry directoryEntry = searcher.FindOne().GetDirectoryEntry();
              directoryEntry.Invoke("ChangePassword", new object[] { sOldPassword, sNewPassword });
              directoryEntry.CommitChanges();
              directoryEntry.Close();
              message = string.Empty;
          }
          catch (Exception exception)
          {
              try
              {
                  message = exception.InnerException.Message;
              }
              catch
              {
                  message = exception.Message;
              }
          }
          return message;
      }
  }
  </code></pre>