Exemplo n.º 1
0
        static public void Run(SecurityContext securityContext, ContextCallback callback, object state)
        {
            if (securityContext == null)
            {
                throw new InvalidOperationException(Locale.GetText(
                                                        "Null SecurityContext"));
            }

            SecurityContext sc       = Thread.CurrentThread.ExecutionContext.SecurityContext;
            IPrincipal      original = Thread.CurrentPrincipal;

            try {
                if (sc.IdentityToken != IntPtr.Zero)
                {
                    Thread.CurrentPrincipal = new WindowsPrincipal(new WindowsIdentity(sc.IdentityToken));
                }

                // FIXME: is the security manager isn't active then we may not have
                // a compressed stack (bug #78652)
                if (securityContext.CompressedStack != null)
                {
                    CompressedStack.Run(securityContext.CompressedStack, callback, state);
                }
                else
                {
                    callback(state);
                }
            }
            finally {
                if ((original != null) && (sc.IdentityToken != IntPtr.Zero))
                {
                    Thread.CurrentPrincipal = original;
                }
            }
        }
        /// <summary>Runs the specified method in the specified security context on the current thread.</summary>
        /// <param name="securityContext">The <see cref="T:System.Security.SecurityContext" /> to set.</param>
        /// <param name="callback">The <see cref="T:System.Threading.ContextCallback" /> delegate that represents the method to run in the specified security context.</param>
        /// <param name="state">The object to pass to the callback method.</param>
        /// <exception cref="T:System.InvalidOperationException">
        ///   <paramref name="securityContext" /> is null.-or-<paramref name="securityContext" /> was not acquired through a capture operation -or-<paramref name="securityContext" /> has already been used as the argument to a <see cref="M:System.Security.SecurityContext.Run(System.Security.SecurityContext,System.Threading.ContextCallback,System.Object)" /> method call.</exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
        /// </PermissionSet>
        public static void Run(SecurityContext securityContext, ContextCallback callback, object state)
        {
            if (securityContext == null)
            {
                throw new InvalidOperationException(Locale.GetText("Null SecurityContext"));
            }
            SecurityContext securityContext2 = Thread.CurrentThread.ExecutionContext.SecurityContext;
            IPrincipal      currentPrincipal = Thread.CurrentPrincipal;

            try
            {
                if (securityContext2.IdentityToken != IntPtr.Zero)
                {
                    Thread.CurrentPrincipal = new WindowsPrincipal(new WindowsIdentity(securityContext2.IdentityToken));
                }
                if (securityContext.CompressedStack != null)
                {
                    CompressedStack.Run(securityContext.CompressedStack, callback, state);
                }
                else
                {
                    callback(state);
                }
            }
            finally
            {
                if (currentPrincipal != null && securityContext2.IdentityToken != IntPtr.Zero)
                {
                    Thread.CurrentPrincipal = currentPrincipal;
                }
            }
        }
Exemplo n.º 3
0
        private void Thread_Run_UnmanagedCode()
        {
            bool result = false;

            Assert.IsFalse(success, "pre-check");
            try {
                CompressedStack cs = GetCompressedStackUnmanaged();
                // run with the captured security stack (i.e. deny unmanaged)
                CompressedStack.Run(cs, new ContextCallback(Callback), true);
            }
            catch (SecurityException) {
                result = true;
            }
            finally {
                Assert.IsFalse(success, "post-check");
                Assert.IsTrue(result, "Result");
            }
        }
Exemplo n.º 4
0
        public static void RunTest()
        {
            CompressedStack compressedStack = CompressedStack.Capture();

            Assert.Throws <NullReferenceException>(() => CompressedStack.Run(compressedStack, null, null));

            var    obj         = new object();
            Thread mainThread  = Thread.CurrentThread;
            bool   callbackRan = false;

            CompressedStack.Run(
                compressedStack,
                state =>
            {
                Assert.Same(obj, state);
                Assert.Same(mainThread, Thread.CurrentThread);
                callbackRan = true;
            },
                obj);
            Assert.True(callbackRan);
        }
Exemplo n.º 5
0
 public void Run_Thread()
 {
     // this is because Thread.CurrentThread.GetCompressedStack () returns null for an empty
     // compressed stack while CompressedStack.GetCompressedStack () return "something" empty ;-)
     CompressedStack.Run(Thread.CurrentThread.GetCompressedStack(), new ContextCallback(Callback), true);
 }
Exemplo n.º 6
0
 public void Run_GetCompressedStack()
 {
     Assert.IsFalse(success, "pre-check");
     CompressedStack.Run(CompressedStack.GetCompressedStack(), new ContextCallback(Callback), true);
     Assert.IsTrue(success, "post-check");
 }
Exemplo n.º 7
0
 public void Run_Null()
 {
     CompressedStack.Run(null, new ContextCallback(Callback), true);
 }
Exemplo n.º 8
0
 [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // desktop framework throws ArgumentException
 public static void RunTest_SkipOnDesktopFramework()
 {
     Assert.Throws <ArgumentNullException>(() => CompressedStack.Run(null, state => { }, null));
 }