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

  • Home
  • SEARCH
  • 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 8239625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:12:45+00:00 2026-06-07T20:12:45+00:00

EDIT: I’ve posted the solution below. I know you don’t like these type of

  • 0

EDIT: I’ve posted the solution below.

I know you don’t like these type of questions, but i’ve been struggling with this issue for half a day now.

I’ve written a C# code that fetches user attributes from our Active Directory using LDAP, the code works well.

The code is as follows:

        DirectoryEntry dirEnt = new DirectoryEntry("LDAP://dc=dom,dc=int");
        DirectorySearcher adSearch = new DirectorySearcher(dirEnt);
        adSearch.SearchScope = SearchScope.Subtree;
        adSearch.PageSize = 10000;
        adSearch.Filter = "(&(objectClass=user))";
        SearchResultCollection sColl = adSearch.FindAll();

        foreach (SearchResult sResult in sColl)
        {
            string sConn = sResult.Properties["distinguishedName"][0].ToString();
            DirectoryEntry dirEnt2 = new DirectoryEntry("LDAP://" + sConn);
            ... 
            // dirEnt2 contains ALL attributes for the user
        }

I’m trying to port this code to Java, but it seems like that the technique I used in C# does not work too well in Java.

Using the following code

DirContext context;
ArrayList<String> nList = new ArrayList<String>();
Hashtable env = new Hashtable();
String username = ...;
String password = ...;

try {
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapUri);

    try {
       context   = new InitialDirContext(env);
     } catch (NamingException e) {
        throw new RuntimeException(e);
     }

    SearchControls ctrl = new SearchControls();
    ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration enumeration = context.search("", "(objectClass=user)",
                                                   ctrl);
    while (enumeration.hasMore()) {
        SearchResult result = (SearchResult) enumeration.next();
        Attributes attribs = result.getAttributes();
        NamingEnumeration values = ((BasicAttribute) 
                                     attribs.get("distinguishedName")).getAll();
        while (values.hasMore()) {
            nList.add(values.next().toString());
            }
        }

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

    for (String sVar : nList ){
        Hashtable env2 = new Hashtable();
        env2.put(Context.SECURITY_PRINCIPAL, username);
        env2.put(Context.SECURITY_CREDENTIALS, password);
        env2.put(Context.INITIAL_CONTEXT_FACTORY, 
                 "com.sun.jndi.ldap.LdapCtxFactory");
        env2.put(Context.PROVIDER_URL, "ldap://DOM/" + sVar);
        Attributes attrs = null;
        try {
            context   = new InitialDirContext(env2);
            attrs = context.getAttributes(sVar);
        } catch (NamingException e) {
            System.out.println(e.toString());
            continue;
        }

        System.out.println(attrs.toString());
    }

Yields that attrs only contains BASIC attributes regarding the user (such as samaccountname, displayname, etc)
and no ’email’, ‘telephone’ or any other similar attributes.

Any help on the issue is blessed!

  • 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-06-07T20:12:47+00:00Added an answer on June 7, 2026 at 8:12 pm

    Here’s the solution, sorry for the messy code/formatting

    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.*;
    import javax.naming.ldap.*;
    
    public class UserFetch {
        public static void main(String[] args) {
            try{
                // Activate paged results
                byte[] cookie = null;
                int count=0;
                int total;
    
                Hashtable env = new Hashtable();
    
                env.put(Context.INITIAL_CONTEXT_FACTORY, 
                "com.sun.jndi.ldap.LdapCtxFactory");
                env.put(Context.REFERRAL, "follow");
                env.put(Context.SECURITY_AUTHENTICATION, "Simple");
                env.put(Context.SECURITY_PRINCIPAL, "USERNAME@DOM.COM");
                env.put(Context.SECURITY_CREDENTIALS, "PASSWORD");
                env.put(Context.PROVIDER_URL, "ldap://DOM.COM:389");
                LdapContext ctx = new InitialLdapContext(env, null);
    
                ctx.setRequestControls(new Control[]{ 
                    new PagedResultsControl(10000, Control.CRITICAL) });
    
                do {
                    // Perform the search
                    NamingEnumeration results =
                    ctx.search("dc=DOM,dc=COM", "(&(objectclass=user)(employeeNumber=*))", getSimpleSearchControls());
    
                    // Iterate over a batch of search results
                    while (results != null && results.hasMore()) {
                        // Display an entry
                        SearchResult entry = (SearchResult)results.next();
                        Attributes attrs = entry.getAttributes ();
                        System.out.println(attrs.get("SAMAccountName")); // Username
                        System.out.println("Firstname: " + 
                        attrs.get("givenname")); // firstname
                        System.out.println("Lastname: " + attrs.get("sn")); // lastname
                        System.out.println("EmployeeID " + attrs.get("employeeID"));
                        System.out.println("EmployeeNumber: " + 
                        attrs.get("employeeNumber"));
                        // Handle the entry's response controls (if any)
                    }
                    // Examine the paged results control response 
                    Control[] controls = ctx.getResponseControls();
                    if (controls != null) {
                        for (int i = 0; i < controls.length; i++) {
                            if (controls[i] instanceof PagedResultsResponseControl) {
                                PagedResultsResponseControl prrc =
                                (PagedResultsResponseControl)controls[i];
                                total = prrc.getResultSize();
                                cookie = prrc.getCookie();
                            } else {
                                // Handle other response controls (if any)
                            }
                        }
                    }
    
                    // Re-activate paged results
                    ctx.setRequestControls(new Control[]{
                        new PagedResultsControl(10000, cookie, Control.CRITICAL) });
                } while (cookie != null);
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static SearchControls getSimpleSearchControls() {
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            searchControls.setTimeLimit(30000);
            String[] attrIDs =
            { "SAMAccountName", "sn", "givenname", "employeeID", 
                "employeeNumber" };
    
            searchControls.setReturningAttributes(attrIDs);
            return searchControls;
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I think I figured out the solution, but I'm still interested to know
EDIT: solved, I know how but I don't understand why. I changed variables declaration
Edit: The below question was answered by this . I have a new updated
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT Leaving this for posterity, but nearly a year later, to get down voted,
EDIT: This question was exceptionally dumb and made me look like a script kiddie,
EDIT: Updated thanks to @daroczig's lovely answer below. However, test 2 still feels like
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know

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.