Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6037575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:05:39+00:00 2026-05-23T06:05:39+00:00

Here is my code currently. I’m making a java program that seaches Active Directory

  • 0

Here is my code currently. I’m making a java program that seaches Active Directory to determine what policies a user/computer has applied. This is currently working as followed. I will next add functionality to add policies to a user. However when checking the policies below no results are yielded if the user does not exist and also if the user has no policies. What I cant figure out is how to determine if the user does not exist? Any help would be appreciated.

public class memberOf   {

    ArrayList results;

    memberOf(String computerName){

        Hashtable env = new Hashtable();
        //String adminName = "CN=Administrator,CN=Users,DC=ANTIPODES,DC=COM";
        //String adminPassword = "XXXXXXX";
        String ldapURL = "n";
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        //set security credentials, note using simple cleartext authentication
        env.put(Context.SECURITY_AUTHENTICATION,"simple");


        env.put(Context.SECURITY_PRINCIPAL,"u");
        System.out.println("Enter password");
        Scanner in = new Scanner(System.in);
        String password = in.nextLine();



        env.put(Context.SECURITY_CREDENTIALS,password);
        //env.put(Context.SECURITY_PROTOCOL, "ssl");



        //connect toSdomain controller
        env.put(Context.PROVIDER_URL,ldapURL);

        try {

            //Create the initial directory context
            LdapContext ctx = new InitialLdapContext(env,null);


            //Create the search controls        
            SearchControls searchCtls = new SearchControls();

            //Specify the search scope
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            //specify the LDAP search filter
            String searchFilter= "CN="+computerName;

            //Specify the Base for the search
            String searchBase = "DC=n,DC=o";

            //initialize counter to total the groups
            int totalResults = 0;


            //Specify the attributes to return
            String returnedAtts[]={"memberOf"};
            searchCtls.setReturningAttributes(returnedAtts);

            //Search for objects using the filter
            NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);


            results = new ArrayList();
                while (answer.hasMoreElements()) {

                    SearchResult sr = (SearchResult)answer.next();

                    Attributes attrs = sr.getAttributes();


                    try {

                        for (NamingEnumeration ae = attrs.getAll();ae.hasMore();) {
                            Attribute attr = (Attribute)ae.next();                          

                            for (NamingEnumeration e = attr.getAll();e.hasMore();totalResults++) {

                                String tempStr = (String)(e.next());
                                int start = tempStr.indexOf("_");
                                int end = tempStr.indexOf(",");
                                tempStr=tempStr.substring(start, end);                          
                                results.add(totalResults,tempStr);                              

                            }

                        }

                    }    
                    catch(Exception e){
                        e.printStackTrace();
                    }                   

                }
        ctx.close();        
        }       
        catch (NamingException e) {
            e.printStackTrace();
        }   

    }
    public ArrayList getResults(){
        System.out.println(results.size());
        if(results.size()==0){
            results.add(0, "No Groups");
        }
        return(results);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:05:40+00:00Added an answer on May 23, 2026 at 6:05 am

    You can’t find it that way. You need to know an attribute to search for in the user (upn, samAccountName, etc), find them that way, and use the backlinked attribute in the user object to find their policies.

    It looks like you are doing the reverse – looking at the policy and asking “Who is a member of this policy”. That works great – but obviously cannot differentiate between

    • user exists but is not member
    • user doesn’t exist at all.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
We have some old C code here that's built with nmake. Is there an
I have a piece of code here that i really could use some help
Here's the code I currently have. <!DOCTYPE html> <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8/>
This is beyond both making sense and my control. That being said here is
Haskell here we go! Okay so here is an example of the code currently
so as the title says..... here is the code I currently wrote thinking it
Currently my code results in a view that looks like this: Lessons Start Now!
I'm currently working on a website that you'll find here: http://steadfastdesignfirm.com/rgw/ . I used
Edit: The code here still has some bugs in it, and it could do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.