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?
Sample Code:
private Properties properties;
private DirContext dirContext;
private SearchControls searchCtls;
private String[] returnAttributes = { "sAMAccountName", "givenName", "cn", "mail"};
private String domainBase;
private String baseFilter = "(&((&(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<SearchResult> 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;
}
I have done search using this this.dirContext.search(base, filter, this.searchCtls);
How can I know user is active or inactive?
I found google to get active
users (!(useraccountcontrol:1.2.840.113556.1.4.803:=2))
and deactive
users (useraccountcontrol:1.2.840.113556.1.4.803:=2)
How to Implements those two in JAVA code.