How do i authenticate against LDAP and return the errors when trying to authenticate against.. Like wrong password

Posted on 2012-09-04
Medium Priority
Last Modified: 2012-09-10
How do i authenticate against LDAP and return the errors when trying to authenticate against.. Like wrong password
Question by:cbrune

Accepted Solution

cubaman_24 earned 2000 total points
ID: 38368790
Use this code:

// create a "principal context" - e.g. your domain (could be machine, too)
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");

Open in new window


By the way, you don't need to inform the user if the user name is wrong or if the password is wrong, just show a message saying "Authentication failed". It will help in case someone tries a brute force attack against your system ;-)

Best regards.
LVL 16

Expert Comment

ID: 38368802
If this is a web app you can use the build in provider functions.

public bool ValidateUser(string userName, string password)
    return _provider.ValidateUser(userName, password);

Here I'm validating what was entered into a form by calling into the provider defined in the web.config (and pointed to LDAP using a connection string).

I suspect you can use role providers outside the web app world as well, by declaring them explicitly.

You can also hook into LDAP directly. I've used it to determine role memberships, but I haven't used it to validate a user. Maybe you can do that, as well? Below I'm hooking into LDAP.

public static PrincipalContext GlobalPrincipalContext
        if (null == _GlobalPrincipalContext)
            String ADServer = ConfigurationManager.AppSettings["Server"];
            String ADContainer = ConfigurationManager.AppSettings["RBAC.Container"];
            String ADAdminUsername = ConfigurationManager.AppSettings["Username"];
            String ADAdminPassword = ConfigurationManager.AppSettings["Password"];

            _GlobalPrincipalContext = new PrincipalContext(ContextType.Domain, ADServer, ADContainer, ADAdminUsername, ADAdminPassword);

        return _GlobalPrincipalContext;


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question