R
Reader
I have an application that allows a user to enter a user name, user password, and the domain or machine name. From this information I would like to verify the user account and password is valid. This must work for either a domain or a local machine account. I have tried to find examples from the web and it seems every example that I have found does not work or produces odd results.
I have tested each of these on Windows 2000, Windows XP, Windows 2003. But I have not found a solution that works for all of them consitantly. Actually the three attempts do not even return the same results. Some may return success while another returns false for the same user!!. Then on say XP it is the opposite where one returns false but the other will work and then on some accounts it just does not work at all.
I will even take help in determining why these are not acting the same on all servers.
The authUserLocal does not seem to work all of the time.
static bool authUserLocal(string username, string password)
{
string path = "WinNT://" + Environment.MachineName + ",computer";
DirectoryEntry entry = new DirectoryEntry(path, username, password);
try
{
Object o = entry.NativeGuid;
return true;
}
catch
{
return false;
}
}
static bool authUserAD(string username,string password)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + Environment.GetEnvironmentVariable("USERDNSDOMAIN").ToString(),username,password);
try
{
Object o = entry.NativeGuid;
return true;
}
catch
{
return false;
}
}
and this is another attempt..
public class UserLogon
{
[DllImport("advapi32.dll")]
public static extern bool LogonUser(string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
//CloseHandle parameters. When you are finished,
//free the memory allocated for the handle.
[DllImport("kernel32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public static bool logonUser(string username, string domain, string userpassword)
{
//LogonUser parameters
IntPtr tokenHandle = IntPtr.Zero;
try
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;
//Call LogonUser to obtain a
//handle to an access token
bool returnValue = true;
returnValue = LogonUser(username, domain,
userpassword,
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if(false == returnValue)
{
Console.Write("LogonUser failed...");
return false;
}
CloseHandle(tokenHandle);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return true;
}
}
public class ClassAuth
{
[DllImport("C:\\WINNT\\System32\\advapi32.dll")]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);
[DllImport("C:\\WINNT\\System32\\Kernel32.dll")]
public static extern int GetLastError();
public bool SetLogin(string uid,string pwd) //,HttpContext context)
{
try
{
int token1;
bool loggedOn = LogonUser(uid,".",pwd,2,0,out token1);
IntPtr token2 = new IntPtr(token1);
WindowsIdentity wi = new WindowsIdentity(token2);
//WindowsPrincipal wp = new WindowsPrincipal(wi);
//HttpContext.Current.User = wp;
return true;
}
catch(Exception exp)
{
MessageBox.Show( exp.Message );
return false;
}
}
}
I have tested each of these on Windows 2000, Windows XP, Windows 2003. But I have not found a solution that works for all of them consitantly. Actually the three attempts do not even return the same results. Some may return success while another returns false for the same user!!. Then on say XP it is the opposite where one returns false but the other will work and then on some accounts it just does not work at all.
I will even take help in determining why these are not acting the same on all servers.
The authUserLocal does not seem to work all of the time.
static bool authUserLocal(string username, string password)
{
string path = "WinNT://" + Environment.MachineName + ",computer";
DirectoryEntry entry = new DirectoryEntry(path, username, password);
try
{
Object o = entry.NativeGuid;
return true;
}
catch
{
return false;
}
}
static bool authUserAD(string username,string password)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + Environment.GetEnvironmentVariable("USERDNSDOMAIN").ToString(),username,password);
try
{
Object o = entry.NativeGuid;
return true;
}
catch
{
return false;
}
}
and this is another attempt..
public class UserLogon
{
[DllImport("advapi32.dll")]
public static extern bool LogonUser(string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
//CloseHandle parameters. When you are finished,
//free the memory allocated for the handle.
[DllImport("kernel32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public static bool logonUser(string username, string domain, string userpassword)
{
//LogonUser parameters
IntPtr tokenHandle = IntPtr.Zero;
try
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;
//Call LogonUser to obtain a
//handle to an access token
bool returnValue = true;
returnValue = LogonUser(username, domain,
userpassword,
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if(false == returnValue)
{
Console.Write("LogonUser failed...");
return false;
}
CloseHandle(tokenHandle);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return true;
}
}
public class ClassAuth
{
[DllImport("C:\\WINNT\\System32\\advapi32.dll")]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);
[DllImport("C:\\WINNT\\System32\\Kernel32.dll")]
public static extern int GetLastError();
public bool SetLogin(string uid,string pwd) //,HttpContext context)
{
try
{
int token1;
bool loggedOn = LogonUser(uid,".",pwd,2,0,out token1);
IntPtr token2 = new IntPtr(token1);
WindowsIdentity wi = new WindowsIdentity(token2);
//WindowsPrincipal wp = new WindowsPrincipal(wi);
//HttpContext.Current.User = wp;
return true;
}
catch(Exception exp)
{
MessageBox.Show( exp.Message );
return false;
}
}
}