private static void SecurityExample() { ProxyType highSecurityObject = new ProxyType(); highSecurityObject.AttemptAccess("High"); // Works OK PermissionSet grantSet = new PermissionSet(PermissionState.None); grantSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); AppDomain lowSecurityAppDomain = AppDomain.CreateDomain("LowSecurity", null, new AppDomainSetup() { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory }, grantSet, null); ProxyType lowSecurityObject = (ProxyType)lowSecurityAppDomain.CreateInstanceAndUnwrap(typeof(ProxyType).Assembly.ToString(), typeof(ProxyType).FullName); lowSecurityObject.DoSomething(highSecurityObject); Console.ReadLine(); }
// This method executes in the low-security AppDomain public void DoSomething(ProxyType highSecurityObject) { AttemptAccess("High->Low"); // Throws // Attempt access from the high-security AppDomain via the low-security AppDomain: Throws highSecurityObject.AttemptAccess("High->Low->High"); // Have the high-security AppDomain via the low-security AppDomain queue a work item to // the thread pool normally (without suppressing the execution context): Throws highSecurityObject.AttemptAccessViaThreadPool(false, "TP (with EC)->High"); // Wait a bit for the work item to complete writing to the console before starting the next work item Thread.Sleep(1000); // Have the high-security AppDomain via the low-security AppDomain queue a work item to // the thread pool suppressing the execution context: Works OK highSecurityObject.AttemptAccessViaThreadPool(true, "TP (no EC)->High"); }