How to validate a username + password of a domain user in c#

This code shows an example of how to validate a domain username password programmatically.


string username = "...";
string password = "...";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
if (principalContext.ValidateCredentials(username, password))
{
	Console.WriteLine($"Authenticated user: {username}");
}
else
{
    Console.WriteLine($"Authentication failed for user: {username}");
}

Post a Comment

Previous Post Next Post