Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

Am I connecting to LDAP and authenticating correctly?

Attached is my code for authenticating to an LDAP server. I'm getting an:

Error authenticating user. Unknown error (0x80005000)

2 Questions: 1st, as my code is written, is this a successful way to authenticate or test authentication? 2nd, if the answer to my 1st question is yes, then shouldn't this line in the code:  "object nativeObject = entry.NativeObject;", if it succeeds, indicate successful authentication? This line: "string test = "LDAP://" + domain + username + pwd;" was just me doing some testing and can be ingnored. Am I going about this in the most generic / simplest way? My users enter their username and password and I just need to verify the match. In other words a match equals authentication for my purposes.

My web app. is written in ASP .NET 3.5 [C#]






    public bool IsAuthenticated(string domain, string username, string pwd)
    {
        bool authentic = false;
        try
        {
            string test = "LDAP://" + domain + username + pwd;
            DirectoryEntry entry = new DirectoryEntry("LDAP:\\" + domain, username, pwd);
            try
            {
                object nativeObject = entry.NativeObject;
                //lblConfirmationMsg.Text = "Successful LDAP Authentication! :)";
                //pnlUpdateConfirmation.Visible = true;
                authentic = true;
            }
            catch (DirectoryServicesCOMException dsce)
            {
                lblConfirmationMsg.Text = "Un-Successful LDAP Authentication! " + dsce.Message.ToString();
                pnlUpdateConfirmation.Visible = true;
            }
            return authentic;
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }
    }

Open in new window

Avatar of guru_sami
guru_sami
Flag of United States of America image

You might want to check this document. Code in Step #3 is what I have used in the past.
For connectionstring you might want to ask your IT admin. You might need the domain name included in the connectionstring.
Here are some variations.
Avatar of Michael Sterling

ASKER

@guru_sami: Thank you. I will look at the variations. Doesn't this line:

DirectoryEntry entry = new DirectoryEntry("LDAP:\\" + domain, username, pwd)

include the domain name in the connection string?
Ahh...you are manually doing it...I think the username you pass to that constructor should include domain as well e.g.
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,domainAndUsername, pwd);

Open in new window

here's my new code. i'm still receiving the same error. any suggestions?

    public bool IsAuthenticated(string domain, string username, string pwd)
    {
        bool authentic = false;
        try
        {
            //string test = "LDAP://" + domain + username + pwd;
            //DirectoryEntry entry = new DirectoryEntry("LDAP:\\" + domain, username, pwd);
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry("LDAP://",
                                                       domainAndUsername,
                                                         pwd);
            try
            {
                object nativeObject = entry.NativeObject;
                //lblConfirmationMsg.Text = "Successful LDAP Authentication! :)";
                //pnlUpdateConfirmation.Visible = true;
                authentic = true;
            }
            catch (DirectoryServicesCOMException dsce)
            {
                lblConfirmationMsg.Text = "Un-Successful LDAP Authentication! " + dsce.Message.ToString();
                pnlUpdateConfirmation.Visible = true;
            }
            return authentic;
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will try this today
Thank you.