[DllImport("advapi32.dll")]
private extern static int LogonUser(string lpszUsernme, string lpszDomain, string lpszPassword, LogonType dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll")]
private extern static bool CloseHandle(IntPtr hObject);
public enum LogonType
{
LOGON32_LOGON_INTERACTIVE = 2,
LOGON32_LOGON_NETWORK = 3,
LOGON32_LOGON_BATCH = 4,
LOGON32_LOGON_SERVICE = 5,
LOGON32_LOGON_UNLOCK = 7,
LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
LOGON32_LOGON_NEW_CREDENTIALS = 9
}
/// <summary>
/// Determines whether [is NT password valid] [the specified username].
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <returns>
/// <c>true</c> if [is NT password valid] [the specified username]; otherwise, <c>false</c>.
/// </returns>
public bool IsNTPasswordValid(string username, string password)
{
return IsNTPasswordValid(username, password, string.Empty);
}
/// <summary>
/// Determines whether [is NT password valid] [the specified username].
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="domain">The domain.</param>
/// <returns>
/// <c>true</c> if [is NT password valid] [the specified username]; otherwise, <c>false</c>.
/// </returns>
public bool IsNTPasswordValid(string username, string password, string domain)
{
IntPtr Token = new IntPtr();
LogonUser(username, domain, password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, ref Token);
CloseHandle(Token);
return Token.ToInt32() != 0;
}