Results 1 to 6 of 6
  1. #1
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Question I need Help about "getAttributes" Method

    Finally i can connect Active Directory and i can get the username with this code :

    Java Code:
     ctx1 = new InitialDirContext(env1);
                    sct  = new SearchControls();
                    sct.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    sct.setReturningAttributes(attrList);
                    srt = ctx1.search("dc=sf,dc=yildiz,dc=edu,dc=tr","(mssfuname=" + userid + ")",sct);
                    if (srt.hasMore()) {
                    sr = (SearchResult) srt.next();
                    sr.getAttributes().get(userid).toString();
    Question is i can get the username (with userid), how to get the other data (like e-mail, faculty, date etc.) ???
    Best Regards..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Can you define all the classes that your code is using?
    And the context.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Norm: It's LDAP through JNDI.
    OP: You'll need to read up on how LDAP queries are structured I suspect.

  4. #4
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    @norm yeah, i did..

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    But you didn't post it so others could see it.

  6. #6
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    You are right, sorry...

    Java Code:
       public void ldapBul(String userid) {
            DirContext              ctx1;
            Hashtable  		env1;
            SearchControls 		sct;
            NamingEnumeration       srt;
            SearchResult 		sr = null;
            String 			attrList[] = { "cn" };
            
            env1 = new Hashtable();
            env1.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env1.put(Context.PROVIDER_URL, "ldap://***.***.*.**"); 
            env1.put(Context.REFERRAL, "follow");
    	
            try {
                   
                    ctx1 = new InitialDirContext(env1);
                    sct  = new SearchControls();
                    sct.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    sct.setReturningAttributes(attrList);
                    srt = ctx1.search("dc=sf,dc=yildiz,dc=edu,dc=tr","(mssfuname=" + userid + ")",sct);
                    
                    if (srt.hasMore()) {
                        sr = (SearchResult) srt.next();
                    //System.out.println(sr.toString());
                    txtStudentName.setText(sr.getAttributes().toString());
                    
                    txtStudentLastName.setText(sr.getAttributes().toString());
                    sr.getAttributes().get("lname").toString(); 
                    
                        
                    //System.out.println(sr.getAttributes().size());
                    }
            } catch (Exception e) {
                e.printStackTrace();
            }
                    
        }
        
        public String getDnBySamAccountName(String networkUsername, String context, DirContext dctx) throws Exception {
         String subEntry;
    
         try {
              //  A SearchControls object holds information about the scope of a search.
              SearchControls controls = new SearchControls();
              controls.setSearchScope(SearchControls.OBJECT_SCOPE);
    
              //  searchString describes what we're looking for, a person, not a computer, with the specified name.
              String searchString = "(&(objectClass=person)(!(objectClass=computer))(sAMAccountName=" + networkUsername +"))";
    
              //  NamingEnumeration is an interface for lists returned by methods in the javax.naming and javax.naming.directory packages.
              //  In our case, it's going to hold the results of our search.
              NamingEnumeration enumer = dctx.search(context, searchString, controls);
    
              if (enumer!=null) {
                   subEntry = ((SearchResult) enumer.next()).getName();
                   return subEntry;
              }
         }
         catch (NullPointerException e) {
              
         }
         catch (Exception e) {
              System.err.println(e);
         }
         return null;
    }

Similar Threads

  1. Replies: 0
    Last Post: 06-24-2011, 09:14 PM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •