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 8455825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:23:08+00:00 2026-06-10T12:23:08+00:00

I am not able to authenticate a user using LDAP. I have got following

  • 0

I am not able to authenticate a user using LDAP. I have got following details:

URL=ldap://10.10.10.10:389 
LDAP BASE:DC=lab2,DC=ins 
LDAP Bind Account: CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins 
LDAP Bind Account Pw: secret 

I can search a sAMAccountName value using above details, but how to authenticate a user with user name and password?
If you follow my previous questions then you will understand that, I am successfully able to connect to LDAP server but not able to authenticate him.
User to authenticate:

user: someusername
password: somepwd

I am not able to connect to LDAP server with 'somepwd' and how should I use someusername. I am able to search given user as sAMAccountName.

  • 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-10T12:23:10+00:00Added an answer on June 10, 2026 at 12:23 pm

    This is a mashup of stuff I found in various places. It should put you along the correct path if you don’t want to use the UnboundID SDK. This isn’t production quality, you might want to add the SSL stuff in here if your shop supports it.

    public static Boolean validateLogin(String userName, String userPassword) {
        Hashtable<String, String> env = new Hashtable<String, String>();
    
    
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://" + LDAP_SERVER + ":" + LDAP_SERVER_PORT + "/" + LDAP_BASE_DN);
    
        // To get rid of the PartialResultException when using Active Directory
        env.put(Context.REFERRAL, "follow");
    
        // Needed for the Bind (User Authorized to Query the LDAP server) 
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, LDAP_BIND_DN);
        env.put(Context.SECURITY_CREDENTIALS, LDAP_BIND_PASSWORD);
    
        DirContext ctx;
        try {
           ctx = new InitialDirContext(env);
        } catch (NamingException e) {
           throw new RuntimeException(e);
        }
    
        NamingEnumeration<SearchResult> results = null;
    
        try {
           SearchControls controls = new SearchControls();
           controls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Search Entire Subtree
           controls.setCountLimit(1);   //Sets the maximum number of entries to be returned as a result of the search
           controls.setTimeLimit(5000); // Sets the time limit of these SearchControls in milliseconds
    
           String searchString = "(&(objectCategory=user)(sAMAccountName=" + userName + "))";
    
           results = ctx.search("", searchString, controls);
    
           if (results.hasMore()) {
    
               SearchResult result = (SearchResult) results.next();
               Attributes attrs = result.getAttributes();
               Attribute dnAttr = attrs.get("distinguishedName");
               String dn = (String) dnAttr.get();
    
               // User Exists, Validate the Password
    
               env.put(Context.SECURITY_PRINCIPAL, dn);
               env.put(Context.SECURITY_CREDENTIALS, userPassword);
    
               new InitialDirContext(env); // Exception will be thrown on Invalid case
               return true;
           } 
           else 
               return false;
    
        } catch (AuthenticationException e) { // Invalid Login
    
            return false;
        } catch (NameNotFoundException e) { // The base context was not found.
    
            return false;
        } catch (SizeLimitExceededException e) {
            throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e);
        } catch (NamingException e) {
           throw new RuntimeException(e);
        } finally {
    
           if (results != null) {
              try { results.close(); } catch (Exception e) { /* Do Nothing */ }
           }
    
           if (ctx != null) {
              try { ctx.close(); } catch (Exception e) { /* Do Nothing */ }
           }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been able to finally get python-openid to authenticate a user, but I'm not
Using Codeigniter, I'm able to create a function for user authentication. I have a
Not able to store all binary data values into sqlite3 table using QT. For
Hi I am not able to capture the screen shot using selenium webdriver .
I am not able to start my app in emulator. Following is the message
I am currently using ADFS authentication mechanism to authenticate the user. In that case
I have an application which needs to be able to write to Any User/Current
I am using C# application to authenticate and check Active Directory user which locates
I am trying to authenticate user using OAuth and retrieve the user data. When
I am using omniauth to authenticate a user via twitter, now as mentioned by

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.