Exemple #1
0
        public static void Ejecutar(Funcion a)
        {
            SafeTokenHandle safeTokenHandle;

            try
            {
                const int LOGON32_PROVIDER_DEFAULT = 0;
                //This parameter causes LogonUser to create a primary token.
                const int LOGON32_LOGON_INTERACTIVE = 2;

                // Call LogonUser to obtain a handle to an access token.
//                bool returnValue = LogonUser("mberroteran", "bremat", "mb123456**",
                bool returnValue = LogonUser("administrador", "bremat", "&&admin%%ABC",
                                             LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                                             out safeTokenHandle);
                using (safeTokenHandle)
                {
                    WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                    using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                    {
                        a.DynamicInvoke();
                    }
                }
            }
            catch { }
        }