I
Ian Frawley
Anyone used the WindowsIdentity class with success? I find if I try to change the current identity to one that exists on another machine and then try to perform and action on that machine i.e. restart a service or list the available processes I keep getting access denied exceptions. Here is an example of my code:
[DllImport("advapi32.dll", SetLastError=true)]
public extern static bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[STAThread]
static void Main(string[] args)
{
IntPtr UserToken = new IntPtr(0);
//int UserToken;
bool loggedOn;
try
{
loggedOn = LogonUser(
"Administrator",
Dns.GetHostName(),
"",3,0, ref UserToken);
}
catch(Exception ex)
{
throw ex;
}
if(loggedOn)
{
WindowsIdentity SystemMonitorUser = new WindowsIdentity(UserToken);
WindowsImpersonationContext ImpersonatedUser = SystemMonitorUser.Impersonate();
Console.WriteLine(SystemMonitorUser.Name);
try
{
Process[] _process;
_process = Process.GetProcesses("192.168.6.236");
foreach (Process p in _process)
{
Console.WriteLine(p.ProcessName.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
ImpersonatedUser.Undo();
}
}
Ian
--
"Life should NOT be a journey to the grave
with the intention of arriving safely in an
attractive and well preserved body,
but rather to skid in sideways,
chocolate in one hand, beer in the other,
body thoroughly used up,
totally worn out and screaming
WOO HOO what a ride!"
[DllImport("advapi32.dll", SetLastError=true)]
public extern static bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[STAThread]
static void Main(string[] args)
{
IntPtr UserToken = new IntPtr(0);
//int UserToken;
bool loggedOn;
try
{
loggedOn = LogonUser(
"Administrator",
Dns.GetHostName(),
"",3,0, ref UserToken);
}
catch(Exception ex)
{
throw ex;
}
if(loggedOn)
{
WindowsIdentity SystemMonitorUser = new WindowsIdentity(UserToken);
WindowsImpersonationContext ImpersonatedUser = SystemMonitorUser.Impersonate();
Console.WriteLine(SystemMonitorUser.Name);
try
{
Process[] _process;
_process = Process.GetProcesses("192.168.6.236");
foreach (Process p in _process)
{
Console.WriteLine(p.ProcessName.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
ImpersonatedUser.Undo();
}
}
Ian
--
"Life should NOT be a journey to the grave
with the intention of arriving safely in an
attractive and well preserved body,
but rather to skid in sideways,
chocolate in one hand, beer in the other,
body thoroughly used up,
totally worn out and screaming
WOO HOO what a ride!"