I want to load an assembly which contains a single class that implements an Interface. The Interface just has one Method: "Run()".
I want to load the class inside a totally restricted AppDomain and prevent the instance from file or Registry access.
This is my Layout:
- Signed Main Project - Loads Assembly, creates an instance and executes the Method Run()
- Signed C# Library - Just contains the IProvider Interface definition
- External C# Library - Implements my Interface in the class MyProvider
This is the code I use to load the external library, create an instance and execute the interface method:
Evidence ev = new Evidence();
ev.AddHostEvidence(new Zone(SecurityZone.Internet));
PermissionSet permSet = SecurityManager.GetStandardSandbox(ev);
StrongName fullTrustAssembly = typeof(Program).Assembly.Evidence.GetHostEvidence<StrongName>();
AppDomainSetup adSetup = new AppDomainSetup()
{
ApplicationBase = Path.GetFullPath(Environment.CurrentDirectory)
};
AppDomain newDomain = AppDomain.CreateDomain("Sandbox", ev, adSetup, permSet, fullTrustAssembly);
Assembly asm = newDomain.Load(System.IO.File.ReadAllBytes("ExternalLib.dll"));
var instance = asm.CreateInstance("ExternalLib.MyProvider", true);
IProvider provider = instance as IProvider;
//Should not work, because my Assembly is accessing a file/Registry or something else
string result = provider.Run("Test");
I want the last line to throw an Exception, because the ExternalLib.dll implements the interface but is accessing my file system or changing the Registry etc
Any Ideas on how to accomplish this?