0

So I have been following the tutorial at https://docs.microsoft.com/en-us/previous-versions/dotnet/framework/code-access-security/how-to-run-partially-trusted-code-in-a-sandbox?redirectedfrom=MSDN to run untrusted code in a test environment with Appdomain, but I'm having trouble with AppDomainSetup constructor and the ApplicationBase property. This is basically the code included in the tutorial :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Reflection;
using System.Runtime.Remoting;



//The Sandboxer class needs to derive from MarshalByRefObject so that we can create it in another
// AppDomain and refer to it from the default AppDomain.
class Sandboxer 
{
    const string pathToUntrusted = @"..\..\..\UntrustedCode\bin\Debug";
    const string untrustedAssembly = "UntrustedCode";
    const string untrustedClass = "UntrustedCode.UntrustedClass";
    const string entryPoint = "IsFibonacci";
    private static Object[] parameters = { 45 };
    static void Main()
    {
        
        AppDomainSetup adSetup = new AppDomainSetup; **//Getting an error here that the constructor doesn't take 0 argumnets**
        adSetup.ApplicationBase = Path.GetFullPath(pathToUntrusted);//Similar error here

       
        PermissionSet permSet = new PermissionSet(PermissionState.None);
        permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

      
        StrongName fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();

        
        AppDomain newDomain = AppDomain.CreateDomain("Sandbox", null, adSetup, permSet, fullTrustAssembly); //another error here,

        
        ObjectHandle handle = Activator.CreateInstanceFrom(
            newDomain, typeof(Sandboxer).Assembly.ManifestModule.FullyQualifiedName,
            typeof(Sandboxer).FullName
            );
        
        Sandboxer newDomainInstance = (Sandboxer)handle.Unwrap();
        newDomainInstance.ExecuteUntrustedCode(untrustedAssembly, untrustedClass, entryPoint, parameters);
    }
    public void ExecuteUntrustedCode(string assemblyName, string typeName, string entryPoint, Object[] parameters)
    {
        
       
        MethodInfo target = Assembly.Load(assemblyName).GetType(typeName).GetMethod(entryPoint);
        try
        {
            
            bool retVal = (bool)target.Invoke(null, parameters);
        }
        catch (Exception ex)
        {
            
            new PermissionSet(PermissionState.Unrestricted).Assert();
            Console.WriteLine("SecurityException caught:\n{0}", ex.ToString());
            CodeAccessPermission.RevertAssert();
            Console.ReadLine();
        }
    }
}

Am I missing a library or something here? If this tutorial is outdated or something, could someone help me with an up-to-date one ? Thanks in advance :) .

  • 1
    AppDomains are not really supported in .NET Core anymore. There are some stub functions and a single global appdomain for rudimentary compatibility. But nothing advanced, including creating custom domains. There is lots of material on the internet, including about the new AssemblyLoadContext. Google for "AppDomain .net core" and you'll be overwhelmed with information ;-) – Christian.K Mar 14 '22 at 08:33

0 Answers0