2
votes

I want to search a user in Active Directory with sAMaccountName,where the sAMaccountName is firstName.substring(0,1)+lastName+ending with any digit. Code Snippet:

 try {
      context=this.getADConnection();

      String returnedAtts[]={"givenName","sn"};
      String sAMAccountNameRegex=sAMAccountName+"\\d*";
      //String sAMAccountNameRegex=sAMAccountName+Pattern.quote("[0-9]*");
      SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setReturningAttributes(returnedAtts);
  searchControls.setReturningAttributes(returnedAtts);
  searchControls.setReturningAttributes(returnedAtts);
  //String searchFilter = "(&(objectClass=user)(givenName=" +firstInitial+"*)(sn="+lastName+"))";
  String searchFilter = "(&(objectClass=user) (|(&(givenName=" +firstInitial+"*)(sn="+lastName+"))(sAMAccountName=("+sAMAccountNameRegex+"))) )";
  logger.info(className + privateMethodName + "  Searching User using filter : [" + searchFilter + "]");
  // Search for objects using the filter
  // Search for objects using the filter
      NamingEnumeration<SearchResult> results = context.search(SAMAccountNamePrePop.adSearchBase, searchFilter, searchControls);
      SearchResult searchResult = null;
      while(results.hasMoreElements()) {
      searchResult = (SearchResult) results.nextElement();
      logger.info(className + privateMethodName + " Search Result : [" + searchResult + "]");
      totalResults++;
      }`

Search filter used is

String searchFilter = "(&(objectClass=user) (|(&(givenName=" +firstInitial+"*)(sn="+lastName+"))(sAMAccountName=("+sAMAccountNameRegex+"))) )";

I have tried with following search filters for sAMaccountName,but none worked and gives following exceptions

  1. String sAMAccountNameRegex=sAMaccountName.Pattern.quote("\\d*");

    Exception is: [invalid escape sequence: [B@755c9b9c]

    after passing values searchfileter looks like:

[(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3\Q\d*\E))) )]

  1. String sAMAccountNameRegex=sAMAccountName+"([0-9]*)$";

    Exception is:[[LDAP: error code 32 - 0000208D: NameErr: DSID-031001E5, problem 2001 (NO_OBJECT), data 0, best match of: '']]

    after passing values searchfileter looks like:

    [(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3([0-9]*)$))) )]

  2. String sAMAccountNameRegex=sAMAccountName+"\d*";

    Exception:[[LDAP: error code 32 - 0000208D: NameErr: DSID-031001E5, problem 2001 (NO_OBJECT), data 0, best match of: '' ]]

    after passing values searchfileter looks like:

[(&(objectClass=user) (|(&(givenName=C*)(sn=BOND3))(sAMAccountName=(CBOND3\d*))) )]

So is it possible to query Ldap where the searchfilter is combination of string and regex?

1
Please format your code properly to make it more readable.aakash
Can you point to the spot in the documentation of Active Directory where it says that LDAP search filters support regular expressions? Because I'm pretty sure they don't.Tomalak
The LDAP Syntax Filters MSDN page, and the simple examples page page from the technet site both indicate that there is no support for regexes in Active Directory search filters, so you're probably fighting a losing battle.Petesh

1 Answers

1
votes

This is how i tried and its working:

I am querying active directory with sAMaccountName*,i get the user and then handling the regex operation in my code locally

Attributes attrs = ((SearchResult) answer.next());
String userId=attrs.get("sAMAccountNAme").toString();
if(userId.matches(sAMAccountNameRegex)){
    //business logic goes here
}