The C++ DWORD type is equivalent to the .NET Int32 type, which is int in C#.
using System;
using System.Runtime.InteropServices;
public class WindowsLogon
{
public enum LogonType
{
Batch = 4,
Interactive = 2,
Network = 3,
NetworkCleartext = 8,
NewCredentials = 9,
Service = 5,
Unlock = 7,
}
public enum LogonProvider
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3,
}
\[DllImport("Advapi32.dll")\]
private static extern bool LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
LogonType dwLogonType,
LogonProvider dwLogonProvider,
out IntPtr phToken);
public static IntPtr LogonUser(
string username, string password, string domain)
{
return LogonUser(username, password, domain,
LogonType.Network, LogonProvider.Default);
}
public static IntPtr LogonUser(
string username, string password, string domain,
LogonType logonType, LogonProvider provider)
{
IntPtr ret = IntPtr.Zero;
if (LogonUser(
username, domain, password,
logonType, provider, out ret))
{
return ret;
}
else
return IntPtr.Zero;
}
static void Main()
{
try
{
Console.Write("Username: ");
string user = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
string domain = Environment.GetEnvironmentVariable("USERDOMAIN");
Console.WriteLine("Domain: {0}", domain);
IntPtr token = LogonUser(user, password, domain);
if (IntPtr.Zero == token)
Console.WriteLine("Logon failed!");
else
Console.WriteLine("Logon OK!");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer