MRebai
MRebai

Reputation: 5474

Could not validate a username and password

I need to validate that the user inputs are corrects to the current session windows. Thus I use the code below :

private void LoginUser(string username, string password)
{
    bool isCredentialValid = false;

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
    {
        isCredentialValid = context.ValidateCredentials(username, password);
    }
    if (isCredentialValid)
    {
        //
    }
    else
    {
        //
    }
}

The problem that I always get false as ValidateCredentials result.

Rq : I'm using .Net 4.5 framework

Upvotes: 1

Views: 792

Answers (1)

error_handler
error_handler

Reputation: 1201

From MSDN http://msdn.microsoft.com/en-us/library/bb154889.aspx

The ValidateCredentials method binds to the server specified in the constructor. If the username and password parameters are null, the credentials specified in the constructor are validated. If no credential were specified in the constructor, and the username and password parameters are null, this method validates the default credentials for the current principal.

In the PrincipalContext constructor you could specify the credentials you want to check as well.

 using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, 
                   container, ContextOptions.SimpleBind, username, password))
{
    return pc.ValidateCredentials(domain + @"\" + username, password,
                               ContextOptions.SimpleBind);
}

Try this one.

Upvotes: 3

Related Questions