title: how to know user is active or inactive in Active Directory
body: <p>I have connected to Active Directory ,so that I was validate user name, but how can I know user is active or inactive in AD?</p>
  
  <p>Sample Code:</p>
  
  <pre><code>            private Properties properties;
              private DirContext dirContext;
              private SearchControls searchCtls;
              private String[] returnAttributes = { "sAMAccountName", "givenName", "cn", "mail"};
              private String domainBase;
              private String baseFilter = "(&amp;((&amp;(objectCategory=Person)(objectClass=User)))";
      public ActiveDirectory(String username, String password, String domainController,String url) {
              properties = new Properties();        
  
              properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
              properties.put(Context.PROVIDER_URL, url);
              properties.put(Context.SECURITY_AUTHENTICATION,"simple");
              properties.put(Context.SECURITY_PRINCIPAL,username+"@"+domainController);
              properties.put(Context.SECURITY_CREDENTIALS, password);
  
              //initializing active directory LDAP connection
              try {
  
                  dirContext = new InitialDirContext(properties);
                  System.out.println("dirContext: "+dirContext);
              } catch (NamingException e) {
                  e.printStackTrace();
                  log.severe(e.getMessage());
              } catch (Exception e) {
                  e.printStackTrace();
              }
                 domainBase = getDomainBase(domainController);
  
              //initializing search controls
              searchCtls = new SearchControls();
              searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
              searchCtls.setReturningAttributes(returnAttributes);
  
          }
  
  
  
       public NamingEnumeration&lt;SearchResult&gt; searchUser(String searchValue, String searchBy, String searchBase) throws NamingException {
                  System.out.println("search value :: "+searchValue);
                  System.out.println("search base111 :: "+ ((null == searchBase) ? domainBase : getDomainBase(searchBase)));
                  String filter = getFilter(searchValue, searchBy);       
                  String base = (null == searchBase) ? domainBase : getDomainBase(searchBase); // for eg.: "DC=myjeeva,DC=com";
                  System.out.println("this.dirContext : "+this.dirContext);
  
  
  
                  return this.dirContext.search(base, filter, this.searchCtls);
              }
        private String getFilter(String searchValue, String searchBy) {
              String filter = this.baseFilter;        
              if(searchBy.equals("email")) {
                  filter += "(mail=" + searchValue + "))";
              } else if(searchBy.equals("username")) {
                  filter += "(samaccountname=" + searchValue + "))";
              }
              System.out.println("filter : "+filter);
              return filter;
          }
  </code></pre>
  
  <p>I have done search using this <code>this.dirContext.search(base, filter, this.searchCtls);</code> </p>
  
  <p>How can I know user is <strong>active</strong> or <strong>inactive</strong>? </p>
  
  <p>I found google to get <code>active</code> users <code>(!(useraccountcontrol:1.2.840.113556.1.4.803:=2))</code> and <code>deactive</code> users  <code>(useraccountcontrol:1.2.840.113556.1.4.803:=2)</code> How to Implements those two in JAVA code.</p>