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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:29:17+00:00 2026-05-19T16:29:17+00:00

I’m working on an ASP.NET project for the first time in about three years;

  • 0

I’m working on an ASP.NET project for the first time in about three years; in the meantime I’ve been working with Python/Django, PHP and Obj-C. Anyways, picked it right back up… except something that is totally killing me right now, and I have a feeling it must be staring me in the face:

I’m trying to bind to an LDAP server, for the purpose of authenticating users. The way it works here is, you bind on your own credentials, use that to find the Distinguished Name of the user you’re authenticating, then you bind again on their DN and their password. If the bind is successful, the password was correct and the user can be authenticated.

Here’s the problem – the first bind (on the fixed credentials, the ones with the ability to search for users and their subtrees) works fine. The search works fine. The second bind fails, no matter what, with the LDAP error INVALID_CREDENTIALS. This happens even when completely valid credentials are supplied.

Here’s the code, with the usernames and passwords redacted, of course…

public static bool Authenticate(string username, string password)  
{  
    bool valid = false;  

    try  
    {  
        LdapDirectoryIdentifier lid = new LdapDirectoryIdentifier("[[REDACTED]]");  
        System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[[REDACTED]]", "[[REDACTED]]");  

        LdapConnection lconn = new LdapConnection(lid);  
        lconn.Bind(cred);  
        SearchRequest request = new SearchRequest("[[REDACTED]]", "cn=" + username, SearchScope.Subtree, new String[] { "dn", "sn" });  
        SearchResponse response = (SearchResponse)lconn.SendRequest(request);  

        SearchResultEntry rslt = response.Entries[0];  
        string userdn = rslt.DistinguishedName;  
        System.Net.NetworkCredential usercred = new System.Net.NetworkCredential(userdn, password);  
        //we're already in try/catch, so if this fails we'll be booted out  
        lconn.Bind(usercred);  
        //otherwise we're all good  
        valid = true;  
    }  
    catch (LdapException e)  
    {  
        if (e.ErrorCode == 0x31) //INVALID_CREDENTIALS  
            throw (e);  
        //otherwise fall through to DB authentication  
    }  

//DB Auth goes here  

    return valid;  

}  

I’ve stepped over this code, reading out the variables and doing the LDAP bind manually (through python-ldap in the python console) and the DN/password combination works fine. What’s going wrong?

  • 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-19T16:29:18+00:00Added an answer on May 19, 2026 at 4:29 pm

    From another SO post. If you want to do a rebind using DN, you need to use AuthType.Basic.

    You may also like to use using wrapping around the LdapConnection so that you won’t forget to close the LdapConnection.

    Below is the updated code that should work. Of course, you better have SSL enabled on your LDAP server since the Basic authentication is used.

    public static bool Authenticate(string username, string password)  
    {  
        bool valid = false;  
    
        try  
        {  
            LdapDirectoryIdentifier lid = new LdapDirectoryIdentifier("[[REDACTED]]");  
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[[REDACTED]]", "[[REDACTED]]");  
    
            string userdn;
            using (LdapConnection lconn = new LdapConnection(lid))
            {
                lconn.Bind(cred);  
                SearchRequest request = new SearchRequest("[[REDACTED]]", "cn=" + username, SearchScope.Subtree, new String[] { "dn", "sn" });  
                SearchResponse response = (SearchResponse)lconn.SendRequest(request);  
    
                SearchResultEntry rslt = response.Entries[0];  
                userdn = rslt.DistinguishedName;  
            }
    
            System.Net.NetworkCredential usercred = new System.Net.NetworkCredential(userdn, password);  
            using (LdapConnection lconn2 = new LdapConnection(lid))
            {
                lconn2.AuthType = AuthType.Basic;
                //we're already in try/catch, so if this fails we'll be booted out  
                lconn2.Bind(usercred);
                //otherwise we're all good  
                valid = true;  
            }
        }  
        catch (LdapException e)  
        {  
            if (e.ErrorCode == 0x31) //INVALID_CREDENTIALS  
                throw (e);  
            //otherwise fall through to DB authentication  
        }  
    
    //DB Auth goes here  
    
        return valid;  
    
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.