I run a macro assembly inside my main application. Macro does not need to access parent assembly. This is the snippet:
Assembly ParentAssembly
{
class c1
{
void RunMacro()
{
System.Security.PermissionSet PS = new System.Security.PermissionSet(PermissionState.None);
PS.AddPermission(new SOME_PERMISSIONS....);
AppDomainSetup ADS = new AppDomainSetup();
ADS.ApplicationBase = "c:";
AppDomain domain = AppDomain.CreateDomain(SomeName, null, ADS, PS);
System.Runtime.Remoting.ObjectHandle handle = Activator.CreateInstanceFrom(domain, typeof(Sandboxer2).Assembly.ManifestModule.FullyQualifiedName, typeof(Sandboxer2).FullName);
Sandboxer2 m = (Sandboxer2)handle.Unwrap();
m.Execute();
}
}
}
I receive this exception:
Attempt by security transparent method 'SandBoxer.Sandboxer2.Execute()' to access security critical method 'System.AppDomain.add_AssemblyResolve(System.ResolveEventHandler)' failed.
Assembly 'Parent Assembly full name...' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted.
My question:
Is there any way to avoid loading parent assembly in child assembly?
In second line of my code, what permissions can solve the problem?
There are some assemblies that will be loaded by AssemblyResolve event of SandBoxer at runtime. Assemblies get loaded from database as binary array or from GAC. They are not fully trusted. I control their behavior with permission objects added at second line of code. Are there special permissions that I have to add for letting them be only loaded as partially trusted assemblies?
I think everything can be done by adding security permissions like second line of code, If I'm misunderstanding the concept, I would be grateful to be guided.
EDIT1: Parent Assembly is the assembly of main application that creates instance of SandBoxr and runs it. Please take a look at SandBoxer2 class and its Execute method:
public class Sandboxer2 : MarshalByRefObject
{
public void Execute()
{
AppDomain ad = AppDomain.CurrentDomain;
ad.AssemblyResolve += MyHandler;
.
.
.
}
}
Inside of Execute method, after ad is instantiated, I used ad.GetAssemblies() and this was the list of all assemblies already loaded. Line number 2 holds ParentAssembly from the very beginning of execution of sandboxer.
- [0] {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [1] {System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [2] {ParentAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [3] {System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [4] {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [5] {MacroBase_IO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [6] {System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [7] {System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [8] {System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
SandBoxer2
, but there are no other assemblies mentioned). It may be better to use specific names (even if not exactly matching what you use in you real code) . – Alexei Levenkov