Actually, using the System.Management namespace allows me to create a scope
with a specific username / password:
ManagementScope scope = new ManagementScope();
scope.Options.Username = computer.Domain.Name + @"\" +
computer.Domain.Username;
scope.Options.Password = computer.Domain.Password;
And using this namespace, I can copy the ini file I need to read off of
their computer to the applications and read it from there without adding an
impersonation class. Although if someone wants the API way ready to copy and
paste:
using System.Runtime.InteropServices;
using System.Security.Principal;
public class Impersonation
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport( "advapi32.dll" )]
public static extern int LogonUserA( String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken );
[DllImport( "advapi32.dll", CharSet = CharSet.Auto, SetLastError =
true )]
public static extern int DuplicateToken( IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken );
[DllImport( "advapi32.dll", CharSet = CharSet.Auto, SetLastError =
true )]
public static extern bool RevertToSelf();
[DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
public static extern bool CloseHandle( IntPtr handle );
public bool Impersonate( String userName, String domain, String
password )
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if ( RevertToSelf() )
{
if ( LogonUserA( userName, domain, password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token ) != 0 )
{
if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
{
tempWindowsIdentity = new WindowsIdentity(
tokenDuplicate );
impersonationContext =
tempWindowsIdentity.Impersonate();
if ( impersonationContext != null )
{
CloseHandle( token );
CloseHandle( tokenDuplicate );
return true;
}
}
}
}
if ( token != IntPtr.Zero )
CloseHandle( token );
if ( tokenDuplicate != IntPtr.Zero )
CloseHandle( tokenDuplicate );
return false;
}
public void UndoImpersonation()
{
impersonationContext.Undo();
}
}