Beispiel #1
0
 internal WindowsImpersonationContext(SafeAccessTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd)
 {
     if (safeTokenHandle.IsInvalid)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken"));
     }
     if (isImpersonating)
     {
         if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(), safeTokenHandle, Win32Native.GetCurrentProcess(), out this.m_safeTokenHandle, 0U, true, 2U))
         {
             throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
         }
         this.m_wi = wi;
     }
     this.m_fsd = fsd;
 }
        internal WindowsImpersonationContext (SafeAccessTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd) {
            if (safeTokenHandle.IsInvalid)
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken"));
            Contract.EndContractBlock();

            if (isImpersonating) {
                if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(),
                                                 safeTokenHandle,
                                                 Win32Native.GetCurrentProcess(),
                                                 ref m_safeTokenHandle,
                                                 0,
                                                 true,
                                                 Win32Native.DUPLICATE_SAME_ACCESS))
                    throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
                m_wi = wi;
            }
            m_fsd = fsd;
        }
        internal static WindowsImpersonationContext SafeImpersonate(SafeTokenHandle userToken, WindowsIdentity wi, ref StackCrawlMark stackMark)
        {
            bool            flag;
            int             hr = 0;
            SafeTokenHandle safeTokenHandle = GetCurrentToken(TokenAccessLevels.MaximumAllowed, false, out flag, out hr);

            if ((safeTokenHandle == null) || safeTokenHandle.IsInvalid)
            {
                throw new SecurityException(Win32Native.GetMessage(hr));
            }
            FrameSecurityDescriptor securityObjectForFrame = SecurityRuntime.GetSecurityObjectForFrame(ref stackMark, true);

            if (securityObjectForFrame == null)
            {
                throw new SecurityException(Environment.GetResourceString("ExecutionEngine_MissingSecurityDescriptor"));
            }
            WindowsImpersonationContext context = new WindowsImpersonationContext(safeTokenHandle, GetCurrentThreadWI(), flag, securityObjectForFrame);

            if (userToken.IsInvalid)
            {
                hr = System.Security.Principal.Win32.RevertToSelf();
                if (hr < 0)
                {
                    Environment.FailFast(Win32Native.GetMessage(hr));
                }
                UpdateThreadWI(wi);
                securityObjectForFrame.SetTokenHandles(safeTokenHandle, (wi == null) ? null : wi.TokenHandle);
                return(context);
            }
            hr = System.Security.Principal.Win32.RevertToSelf();
            if (hr < 0)
            {
                Environment.FailFast(Win32Native.GetMessage(hr));
            }
            if (System.Security.Principal.Win32.ImpersonateLoggedOnUser(userToken) < 0)
            {
                context.Undo();
                throw new SecurityException(Environment.GetResourceString("Argument_ImpersonateUser"));
            }
            UpdateThreadWI(wi);
            securityObjectForFrame.SetTokenHandles(safeTokenHandle, (wi == null) ? null : wi.TokenHandle);
            return(context);
        }
Beispiel #4
0
        internal WindowsImpersonationContext(SafeAccessTokenHandle safeTokenHandle, WindowsIdentity wi, bool isImpersonating, FrameSecurityDescriptor fsd)
        {
            if (safeTokenHandle.IsInvalid)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidImpersonationToken"));
            }
            Contract.EndContractBlock();

            if (isImpersonating)
            {
                if (!Win32Native.DuplicateHandle(Win32Native.GetCurrentProcess(),
                                                 safeTokenHandle,
                                                 Win32Native.GetCurrentProcess(),
                                                 ref m_safeTokenHandle,
                                                 0,
                                                 true,
                                                 Win32Native.DUPLICATE_SAME_ACCESS))
                {
                    throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
                }
                m_wi = wi;
            }
            m_fsd = fsd;
        }
 internal static extern bool GetDescriptorInfo(IntPtr dcs, int index, out PermissionSet granted, out PermissionSet refused, out Assembly assembly, out FrameSecurityDescriptor fsd);