c# - Dynamically load assembly from local file and run with restricted privileges -


what need : read local c# text file, execute method that. i'm doing.

  1. read text file
  2. compile local x.dll csharpcodeprovider
  3. load dll assembly.loadfrom()
  4. then execute method gettype().getmethod().invoke()

it works fine. now, want run code securely, i.e. restrict code accessing file system, network etc. basically, need run minimal privileges.

i tried code restrict plugin access file system , network via appdomain (answer @babar), still not working expected. code in text file still able access file system.

what i'm missing here? other way make work?

the code (for loading , executing assembly)

public class sandboxer {     public static t getresult<t>(string untrustedassemblydirectory, string assemblyfullpath, string classname, string methodname, object[] methodparameters)     {         appdomainsetup adsetup = new appdomainsetup();         adsetup.applicationbase = path.getfullpath(untrustedassemblydirectory);          permissionset permset = new permissionset(permissionstate.none);         permset.addpermission(new securitypermission(securitypermissionflag.execution));          strongname fulltrustassembly = typeof(sandboxer).assembly.evidence.gethostevidence<strongname>();          appdomain newdomain = appdomain.createdomain("sandboxer", null, adsetup, permset, fulltrustassembly);          objecthandle handle = activator.createinstancefrom(             newdomain, typeof(sandboxer).assembly.manifestmodule.fullyqualifiedname,             typeof(sandboxer).fullname             );          sandboxer newdomaininstance = (sandboxer)handle.unwrap();         return newdomaininstance.executeuntrustedcode<t>(assemblyfullpath, classname, methodname, methodparameters);     }      public t executeuntrustedcode<t>(string assemblyname, string typename, string entrypoint, object[] parameters)     {         var method = assembly.loadfrom(assemblyname).gettype(typename).getmethod(entrypoint);         try         {             t retval = (t)method.invoke(null, parameters);             return retval;         }         catch (exception ex)         {             var expmsg = string.empty;             (new permissionset(permissionstate.unrestricted)).assert();             expmsg = "exception :\n{0}" + ex.tostring();             codeaccesspermission.revertassert();             throw new applicationexception(expmsg);         }     } } 


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -