views:

425

answers:

1

Hello Everyone, I have an MVC application that needs to login and verify a user against active directory. I am using the PrincipalContext.ValidateCredentials method but always get a authentication of false.

Connecting to the Server is fine. The problem seems to occur in the ValidateCredentials.

Here is my code:

    public static bool IsAuthenticated(string domain, string username, string pwd) {

        bool IsAuthenticated = false;

        try {
            PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, domain,
                                       "DC=c1w,DC=com");

            username = "c1w\\" + username;

            IsAuthenticated = insPrincipalContext.ValidateCredentials(username, pwd);
        }
        catch (Exception ex)
        {
            //Rethrow this exception
            ExceptionPolicy.HandleException(ex, "Exception Policy");
        }

        return IsAuthenticated;
    }

Anyone know why this would be happening?

Thanks, Billy

+2  A: 

I don't see where you initializes the "pwd" variable Maybe you should use ContextOption in this method to specify exactly the reqired behaviour. Sorry for too broad response but there is no much details in your question

B-Rain
Editted the question to include the entire method. Will try the ContextOption suggestion. Thank you.
Billy Logan
B-Rain,The ContextOption reference pointed me in the right direction. Ended up using the ContextOptions.Negotiate on my call to AD and ContextOptions.SimpleBind on the validate credentials. Simple Bind will work for me since the site will be SSL Secured. Thanks for your help.
Billy Logan
Upvoted both the question and the answer because this helped me in my situation as well. In my case, my dev machine (where login works without context specified) is in the secured zone on the network, but the web server (where login doesn't work without context specified) is in the DMZ. I used the same configuration as @Billy Logan - Negotiate on the call to AD and SimpleBind on the validate call.
arootbeer