impersonar un proceso dentro de un win service

30/05/2005 - 18:07 por Alexandra | Informe spam
Buenos días,
estoy tratando de impersonar un proceso desde un windows
service app.
Les agradecería mucho si alguien me puede ayudar!

Les paso parte del código, si es necesario luego les paso
el resto:
WindowsIdentity wi = WinAccessHelper.LogonUser
("dominio", "usuario", "psw",
WinAccessHelper.LogonType.Interactive);


string exe = "Notepad.exe";
string args = configElement.Parameters;
ProcessStartInfo info = new ProcessStartInfo(exe, args);
info.UseShellExecute = false;
info.WorkingDirectory = Path.GetDirectoryName(exe);


wic = wi.Impersonate();
Process myProcess = Process.Start(info);


if( wic != null )
wic.Undo();


//el metodo LogonUser

[DllImport("advapi32.dll",
private static extern bool LogonUser
(string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);


//

public static WindowsIdentity LogonUser(string
domain, string userName, string password, LogonType
logontype)
{
// initialize tokens
IntPtr pExistingTokenHandle = new
IntPtr(0);
IntPtr pDuplicateTokenHandle = new
IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
pDuplicateTokenHandle =
IntPtr.Zero;
bool returnValue = false;

try
{
if(domain == null ||
domain.Length == 0) domain = Environment.MachineName;

returnValue = LogonUser
(userName,
domain,
password,
(int)logontype,

LOGON32_PROVIDER_DEFAULT,
ref
pExistingTokenHandle);


if (returnValue &&
pExistingTokenHandle != IntPtr.Zero )
{
returnValue =
DuplicateToken(pExistingTokenHandle, (int)
SECURITY_IMPERSONATION_LEVEL.SecurityDelegation, ref
pDuplicateTokenHandle);
//
SECURITY_ATTRIBUTES secAttrs = new SECURITY_ATTRIBUTES();
//
secAttrs.nLength = Marshal.SizeOf(secAttrs);
//
secAttrs.lpSecurityDescriptor = IntPtr.Zero;
// returnValue =
DuplicateTokenEx(pExistingTokenHandle, 0, ref secAttrs,
(int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
(int)TOKEN_TYPE.TokenPrimary, ref pDuplicateTokenHandle);
// did
DuplicateToken fail?
if (returnValue)
{
// create
new identity using new primary token
return new
WindowsIdentity(pDuplicateTokenHandle, "NTLM",
WindowsAccountType.Normal, true);
}
else
throw new
Win32Exception(Marshal.GetLastWin32Error());

}
else

throw new
Win32Exception(Marshal.GetLastWin32Error());
}
catch(Win32Exception wex)

{
//Add detailed description
throw new Win32Exception
(wex.NativeErrorCode,
String.Format
(@"Logon failed for user {0}\{1} for {2} access.{3}
Exception found: {4}.{3}.Machine: {5}.{3}Current Identity:
{6}.", domain, userName, logontype.ToString(),
Environment.NewLine, wex.Message, Environment.MachineName,
WindowsIdentity.GetCurrent().Name));
}

finally
{

// close handle(s)
if (pExistingTokenHandle !
= IntPtr.Zero ) CloseHandle(pExistingTokenHandle);
if (pDuplicateTokenHandle !
= IntPtr.Zero ) CloseHandle(pDuplicateTokenHandle);
}

}

saludos.
 

Leer las respuestas

#1 Tristan
31/05/2005 - 22:34 | Informe spam
¿Y el problema es?

Juan Carlos Badiola
MVP - C#

Preguntas similares