Esempio n. 1
0
        public void WindowsIdentity_()
        {
            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            SecurityContext sc = SecurityContext.Capture();

            Assert.IsNotNull(sc, "Capture");
        }
Esempio n. 2
0
        internal static ExecutionContext Capture(bool captureSyncContext, bool nullOnEmpty)
        {
            ExecutionContext ec = Current;

            if (ec.FlowSuppressed)
            {
                return(null);
            }

            if (nullOnEmpty
#if !MOBILE
                && ec._sc == null
#endif
                && (ec._lcc == null || !ec._lcc.HasInfo))
            {
                return(null);
            }

            ExecutionContext capture = new ExecutionContext(ec);
#if !MOBILE
            if (SecurityManager.SecurityEnabled)
            {
                capture.SecurityContext = SecurityContext.Capture();
            }
#endif
            return(capture);
        }
Esempio n. 3
0
        public static void Run(ContextBody callback)
        {
            callback();
            return;

            //WindowsIdentity.RunImpersonated(SafeAccessTokenHandle.InvalidHandle, () => callback());
            return;

            SecurityContext.Run(SecurityContext.Capture(), delegate(object state) {
                WindowsImpersonationContext context = null;
                try
                {
                    try
                    {
                        context = WindowsIdentity.Impersonate(IntPtr.Zero);
                        callback();
                    }
                    finally
                    {
                        if (context != null)
                        {
                            context.Undo();
                        }
                    }
                }
                catch
                {
                    throw;
                }
            }, null);
        }
Esempio n. 4
0
        public void Capture()
        {
            SecurityContext sc = SecurityContext.Capture();

            Assert.IsNotNull(sc, "Capture");

            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            try
            {
                sc = SecurityContext.Capture();
                Assert.IsNull(sc, "Capture with SuppressFlow");
            }
            finally
            {
                afc.Undo();
            }

            afc = SecurityContext.SuppressFlowWindowsIdentity();
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1");
            try
            {
                sc = SecurityContext.Capture();
                Assert.IsNotNull(sc, "Capture with SuppressFlowWindowsIdentity");
            }
            finally
            {
                afc.Undo();
            }
        }
Esempio n. 5
0
        public static void RunFromRestrictedCasContext(ContextBody callback)
        {
            callback();
            return;

            SecurityContext.Run(SecurityContext.Capture(), delegate(object state) {
                new SecurityPermission(SecurityPermissionFlag.ControlPrincipal | SecurityPermissionFlag.UnmanagedCode).Assert();
                WindowsImpersonationContext context = null;
                try
                {
                    try
                    {
                        context = WindowsIdentity.Impersonate(IntPtr.Zero);
                        CodeAccessPermission.RevertAssert();
                        callback();
                    }
                    finally
                    {
                        if (context != null)
                        {
                            context.Undo();
                        }
                    }
                }
                catch
                {
                    throw;
                }
            }, null);
            return;

            SecurityContext.Run(SecurityContext.Capture(), delegate(object state)
            {
                new SecurityPermission(SecurityPermissionFlag.UnmanagedCode | SecurityPermissionFlag.ControlPrincipal).Assert();
                //WindowsImpersonationContext windowsImpersonationContext = null;
                try
                {
                    try
                    {
                        WindowsIdentity.Impersonate(IntPtr.Zero);
                        //windowsImpersonationContext = WindowsIdentity.Impersonate(IntPtr.Zero);
                        //CodeAccessPermission.RevertAssert();
                        //WindowsIdentity.RunImpersonated(SafeAccessTokenHandle.InvalidHandle, () => callback());
                        //callback();
                    }
                    finally
                    {
                        //if (windowsImpersonationContext != null)
                        //{
                        //    windowsImpersonationContext.Undo();
                        //}
                    }
                }
                catch
                {
                    throw;
                }
            }, null);
        }
 internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
 {
     if (SecurityContext.IsWindowsIdentityFlowSuppressed())
     {
         return(SecurityContext.Capture());
     }
     using (SecurityContext.SuppressFlowWindowsIdentity())
     {
         return(SecurityContext.Capture());
     }
 }
Esempio n. 7
0
        public void Run_SuppressFlow()
        {
            Assert.IsFalse(SecurityContext.IsFlowSuppressed());
            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed());
            try {
                SecurityContext.Run(SecurityContext.Capture(), new ContextCallback(Callback), "Hello world.");
            }
            finally {
                afc.Undo();
            }
        }
Esempio n. 8
0
        public void ExecutionContext_SuppressFlow()
        {
            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            try {
                Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
                Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-3");
                Assert.IsNotNull(SecurityContext.Capture(), "Capture");
            }
            finally {
                afc.Undo();
            }
        }
 internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
 {
     // capture the security context but never flow windows identity
     if (SecurityContext.IsWindowsIdentityFlowSuppressed())
     {
         return(SecurityContext.Capture());
     }
     else
     {
         using (SecurityContext.SuppressFlowWindowsIdentity())
         {
             return(SecurityContext.Capture());
         }
     }
 }
        /// <summary>Captures the execution context from the current thread.</summary>
        /// <returns>An <see cref="T:System.Threading.ExecutionContext" /> object representing the execution context for the current thread.</returns>
        /// <filterpriority>1</filterpriority>
        public static ExecutionContext Capture()
        {
            ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext;

            if (executionContext.FlowSuppressed)
            {
                return(null);
            }
            ExecutionContext executionContext2 = new ExecutionContext(executionContext);

            if (SecurityManager.SecurityEnabled)
            {
                executionContext2.SecurityContext = SecurityContext.Capture();
            }
            return(executionContext2);
        }
Esempio n. 11
0
        public void Copy()
        {
            SecurityContext sc = SecurityContext.Capture();

            Assert.IsNotNull(sc, "Capture");

            SecurityContext copy = sc.CreateCopy();

            Assert.IsNotNull(copy, "Copy of Capture");

            Assert.IsFalse(sc.Equals(copy));
            Assert.IsFalse(copy.Equals(sc));
            Assert.IsFalse(Object.ReferenceEquals(sc, copy));

            SecurityContext copy2nd = copy.CreateCopy();

            Assert.IsNotNull(copy2nd, "2nd level copy of Capture");
        }
Esempio n. 12
0
        public static ExecutionContext Capture()
        {
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;

            if (ec.FlowSuppressed)
            {
                return(null);
            }

            ExecutionContext capture = new ExecutionContext(ec);

#if (!NET_2_1 || MONOTOUCH) && !DISABLE_SECURITY
            if (SecurityManager.SecurityEnabled)
            {
                capture.SecurityContext = SecurityContext.Capture();
            }
#endif
            return(capture);
        }
Esempio n. 13
0
        public static ExecutionContext Capture()
        {
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;

            if (ec.FlowSuppressed)
            {
                return(null);
            }

            ExecutionContext capture = new ExecutionContext(ec);

#if !MOONLIGHT
            if (SecurityManager.SecurityEnabled)
            {
                capture.SecurityContext = SecurityContext.Capture();
            }
#endif
            return(capture);
        }
Esempio n. 14
0
        internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
        {
            SecurityContext securityContext;

            if (SecurityContext.IsWindowsIdentityFlowSuppressed())
            {
                return(SecurityContext.Capture());
            }
            AsyncFlowControl asyncFlowControl = SecurityContext.SuppressFlowWindowsIdentity();

            try
            {
                securityContext = SecurityContext.Capture();
            }
            finally
            {
                ((IDisposable)asyncFlowControl).Dispose();
            }
            return(securityContext);
        }
Esempio n. 15
0
        public static ExecutionContext Capture()
        {
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;

            if (ec.FlowSuppressed)
            {
                return(null);
            }

            ExecutionContext capture = new ExecutionContext(ec);

#if NOT_PFX
            if (SecurityManager.SecurityEnabled)
            {
                capture.SecurityContext = SecurityContext.Capture();
            }
            // TODO: investigate this case
#endif
            return(capture);
        }
Esempio n. 16
0
        internal static ExecutionContext Capture(bool captureSyncContext)
        {
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;

            if (ec.FlowSuppressed)
            {
                return(null);
            }

            ExecutionContext capture = new ExecutionContext(ec);

#if !MOBILE
            if (SecurityManager.SecurityEnabled)
            {
                capture.SecurityContext = SecurityContext.Capture();
            }
#endif

#if !MONOTOUCH
            capture.LogicalCallContext = CallContext.CreateLogicalCallContext(false);
#endif
            return(capture);
        }
Esempio n. 17
0
 public void Run()
 {
     Assert.IsFalse(success, "pre-check");
     SecurityContext.Run(SecurityContext.Capture(), new ContextCallback(Callback), true);
     Assert.IsTrue(success, "post-check");
 }
Esempio n. 18
0
 static void Main()
 {
     SecurityContext.Run(SecurityContext.Capture(),
                         new ContextCallback(Callback), "Hello world.");
 }
Esempio n. 19
0
 private SecurityContext GetSecurityContextUnmanaged()
 {
     return(SecurityContext.Capture());
     // the Deny disappears with this stack frame but we got a capture of it
 }
Esempio n. 20
0
 private void Thread_Run_Empty()
 {
     Assert.IsFalse(success, "pre-check");
     SecurityContext.Run(SecurityContext.Capture(), new ContextCallback(Callback), true);
     Assert.IsTrue(success, "post-check");
 }
Esempio n. 21
0
 internal CimAsyncCancellationDisposable(CimOperation operation)
 {
     this._disposeThreadSafetyLock = new object();
     this._securityContext         = SecurityContext.Capture();
     this._operation = operation;
 }