El TecnoBaúl de Kiquenet

Kiquenet boring stories

Windows impersonation in C#

Posted by kiquenet en 9 agosto 2011

When you have a system and you may want to move files in a secure environment without the users having to know the password to the destination machine. The following functions do this by using maintained username, password and domain from the database which can be encrypted and decrypted. For my case I have used tripledes class to encrypt my parameter values.

 public static void fnSecureCopy(string Source, string Destination, string Dorm, string Pass, string Usr)
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;
        if (RevertToSelf())
            if (LogonUser(Usr, Dorm, clsEncDec.DecryptString(Pass), 
LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token)) { if (DuplicateToken(token, LOGON32_LOGON_INTERACTIVE, ref tokenDuplicate)) { WindowsIdentity identity = new WindowsIdentity(tokenDuplicate); WindowsImpersonationContext context = identity.Impersonate(); if ((context != null)) { CloseHandle(token); CloseHandle(tokenDuplicate); if (!Directory.Exists(Path.GetDirectoryName(Destination))) Directory.CreateDirectory(Path.GetDirectoryName(Destination));
File.Copy(Source, Destination);
clsLogs.fnErrorLog("Impersonated user: " +
WindowsIdentity.GetCurrent().Name, "RepoWeb");
context.Undo(); ClsAudit.Log_Audit("Generate", "Generated Format 31" + Destination); } } } if (token != IntPtr.Zero) CloseHandle(token); if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate); } else { //log error clsLogs.fnErrorLog("Failed to impersonate user: " + Dorm +
@"\" + Usr, "Impersonation to copy file."); } }




Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: