Esempio n. 1
0
 private void Reset()
 {
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this.stateWasChanged = false;
         this.initialState    = false;
         this.needToRevert    = false;
         if (this.tlsContents != null && this.tlsContents.DecrementReferenceCount() == 0)
         {
             this.tlsContents = (Privilege.TlsContents)null;
             Thread.SetData(Privilege.tlsSlot, (object)null);
         }
     }
 }
Esempio n. 2
0
        private void ToggleState(bool enable)
        {
            int num = 0;

            if (!this.currentThread.Equals((object)Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (this.needToRevert)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustRevertPrivilege"));
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                try
                {
                    this.tlsContents = Thread.GetData(Privilege.tlsSlot) as Privilege.TlsContents;
                    if (this.tlsContents == null)
                    {
                        this.tlsContents = new Privilege.TlsContents();
                        Thread.SetData(Privilege.tlsSlot, (object)this.tlsContents);
                    }
                    else
                    {
                        this.tlsContents.IncrementReferenceCount();
                    }
                    Win32Native.TOKEN_PRIVILEGE NewState = new Win32Native.TOKEN_PRIVILEGE();
                    NewState.PrivilegeCount       = 1U;
                    NewState.Privilege.Luid       = this.luid;
                    NewState.Privilege.Attributes = enable ? 2U : 0U;
                    Win32Native.TOKEN_PRIVILEGE PreviousState = new Win32Native.TOKEN_PRIVILEGE();
                    uint ReturnLength = 0;
                    if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref NewState, (uint)Marshal.SizeOf <Win32Native.TOKEN_PRIVILEGE>(PreviousState), out PreviousState, out ReturnLength))
                    {
                        num = Marshal.GetLastWin32Error();
                    }
                    else if (1300 == Marshal.GetLastWin32Error())
                    {
                        num = 1300;
                    }
                    else
                    {
                        this.initialState    = (PreviousState.Privilege.Attributes & 2U) > 0U;
                        this.stateWasChanged = this.initialState != enable;
                        this.needToRevert    = this.tlsContents.IsImpersonating || this.stateWasChanged;
                    }
                }
                finally
                {
                    if (!this.needToRevert)
                    {
                        this.Reset();
                    }
                }
            }
            if (num == 1300)
            {
                throw new PrivilegeNotHeldException(Privilege.privileges[(object)this.luid] as string);
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5 || num == 1347)
            {
                throw new UnauthorizedAccessException();
            }
            if (num != 0)
            {
                throw new InvalidOperationException();
            }
        }