Impersonation of a user. Allows to execute code under another user context. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.
This class is based on the information in the Microsoft knowledge base article http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158 Encapsulate an instance into a using-directive like e.g.: ... using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) ) { ... [code that executes under the new context] ... } ... Please contact the author Uwe Keim (mailto:[email protected]) for questions regarding this class.
Наследование: IDisposable
Пример #1
0
        private void ImpersonateValidUser(string userName, string domain, string password)
        {
            IntPtr zero1 = IntPtr.Zero;
            IntPtr zero2 = IntPtr.Zero;

            try
            {
                if (!Impersonator.RevertToSelf())
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                if ((uint)Impersonator.LogonUser(userName, domain, password, 2, 0, ref zero1) <= 0U)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                if ((uint)Impersonator.DuplicateToken(zero1, 2, ref zero2) <= 0U)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                this.impersonationContext = new WindowsIdentity(zero2).Impersonate();
            }
            finally
            {
                if (zero1 != IntPtr.Zero)
                {
                    Impersonator.CloseHandle(zero1);
                }
                if (zero2 != IntPtr.Zero)
                {
                    Impersonator.CloseHandle(zero2);
                }
            }
        }
Пример #2
0
        public bool Execute()
        {
            Tools.Impersonator imp = null;
            try
            {
                if (this.Impersonate && !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) imp = new Impersonator(Username, Domain, Password);

                List<string> results = new List<string>();
                try
                {
                    if (Sources != null && Sources.Length > 0)
                    {
                        foreach (var item in Sources)
                        {
                            if (!string.IsNullOrEmpty(item))
                            {
                                if (System.IO.File.Exists(item))
                                {
                                    string script = System.IO.File.ReadAllText(item);
                                    results.Add(Execute(script));
                                }
                                else
                                {
                                    results.Add("");
                                }
                            }
                            else
                            {
                                results.Add("");
                            }

                        }

                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(this.Code))
                            results.Add(Execute(this.Code));
                        else
                            results.Add("");
                    }

                }
                catch (Exception e)
                {
                    results.Add(e.ToString());
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e);
                    Console.ResetColor();
                    return false;
                }

                this.CompilerResults = results.ToArray();

                return true;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (this.Impersonate && imp != null) imp.Dispose();

            }
        }