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

Posted on 2012-09-04
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
    LVL 8

    Accepted Solution

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    How to count json data 2 41
    prevent lose focus 6 49 mvc C# 2 27
    C# System Cannot Find File Specified Error 4 34
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now