Re: Impersonate ARRRRRRRRRRRRRRRRRRGG!
From: Ciaran (ciaran_at_theodonnells.plus.com)
Date: 12/18/04
- Previous message: Ciaran: "Re: ArrayConverter example"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 18 Dec 2004 00:59:52 -0000
One this i see wrong is LogonUser returns an int.
Second, the token you get back cant be used for impersonation and needs to
be converted to a primary token.
I have some code around here somewhere and will post it when I find it.
Probably monday
Ciaran
"Ian Frawley" <chocks@away.com> wrote in message
news:hO3nd.3$AM.340@news-1.opaltelecom.net...
> 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!"
>
>
- Previous message: Ciaran: "Re: ArrayConverter example"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|