Beispiel #1
0
        public static bool DoWorkUnderImpersonation(Func <bool> action, DomainAuthentication domainAuthentication)
        {
            if (domainAuthentication == null)
            {
                throw new Exception("Domain credentials must be provided.");
            }

            // Elevate privileges before doing file copy to handle domain security.
            WindowsImpersonationContext impersonationContext = null;
            IntPtr    userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT  = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;

            try
            {
                // File.AppendAllText(@"C:\doc\impersonation.txt", "Windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);

                // Call LogonUser to get a token for the user
                bool loggedOn = LogonUser(domainAuthentication.Username,
                                          domainAuthentication.Domain,
                                          domainAuthentication.Password,
                                          LOGON32_LOGON_INTERACTIVE,
                                          LOGON32_PROVIDER_DEFAULT,
                                          ref userHandle);

                if (!loggedOn)
                {
                    // File.AppendAllText(@"C:\doc\impersonation.txt", "Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
                    return(false);
                }

                // Begin impersonating the user
                impersonationContext = WindowsIdentity.Impersonate(userHandle);

                // File.AppendAllText(@"C:\doc\impersonation.txt", "Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);

                // Run the provided action using the provided user's handle.
                return(action());
            }
            catch (Exception)
            {
                // File.AppendAllText(@"C:\doc\impersonation.txt", "Exception impersonating user: " + ex.Message);
                return(false);
            }
            finally
            {
                // Clean up
                if (impersonationContext != null)
                {
                    impersonationContext.Undo();
                }

                if (userHandle != IntPtr.Zero)
                {
                    CloseHandle(userHandle);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Opens the download window that download the update and execute the installer when download completes.
        /// </summary>
        public static bool DownloadUpdate(UpdateInfoEventArgs args)
        {
            using (var downloadDialog = new DownloadUpdateDialog(args))
            {
                try
                {
                    if (UseImpersonation)
                    {
                        DomainAuthentication domainAuthentication = new DomainAuthentication(ImpersonationUser, ImpersonationPassword, ImpersonationDomain);
                        return(Impersonator.DoWorkUnderImpersonation(() => downloadDialog.ShowDialog().Equals(DialogResult.OK), domainAuthentication));
                    }
                    else
                    {
                        return(downloadDialog.ShowDialog().Equals(DialogResult.OK));
                    }
                }
                catch (TargetInvocationException)
                {
                }
            }

            return(false);
        }