【问题标题】:AppDomain.DoCallBack requires ReflectionPermission?AppDomain.DoCallBack 需要 ReflectionPermission?
【发布时间】:2012-02-15 19:31:41
【问题描述】:

我有这个类,我在 AppDomain 中创建了它的实例,但没有权限但 SecurityPermissionFlag.Execute:

class IsolationEntryPoint : MarshalByRefObject
{
    // main is the original AppDomain with all the permissions
    public void Enter(AppDomain main)
    {
        // these work correctly
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Host: " + main.FriendlyName);

        // the exception is thrown here
        main.DoCallBack(this.MyCallBack);
    }

    public void MyCallBack()
    {
        Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
    }
}

奇怪的是,我在 DoCallback 行中得到 SecurityException:

请求类型的许可 'System.Security.Permissions.ReflectionPermission, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 失败了。

MSDN 说this 关于 AppDomain.DoCallBack 的权限要求:

ReflectionPermission 通过后期绑定的机制调用时,例如 作为 Type.InvokeMember。

调用未使用Type.InvokeMember 之类的内容,为什么会出现异常?

编辑

为了清楚起见,这里是我用来创建带有隔离对象的 AppDomain 的代码:

    [STAThread]
    static void Main(string[] args)
    {

        var setup = new AppDomainSetup();
        setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

        var evidence = new Evidence();

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

        var domain = AppDomain.CreateDomain(
            "isolationDomain",
            evidence,
            setup,
            permissions);

        var handle = Activator.CreateInstanceFrom(
            domain, typeof(IsolationEntryPoint).Assembly.ManifestModule.FullyQualifiedName,
            typeof(IsolationEntryPoint).FullName);

        var instance = (IsolationEntryPoint)handle.Unwrap();

        instance.Enter(AppDomain.CurrentDomain);
    }

这两段代码是我的完整应用程序,没有别的(所以异常应该很容易重现)。

感谢您的帮助

【问题讨论】:

    标签: c# .net remoting appdomain isolation


    【解决方案1】:

    解决方案实际上很简单:您错过了将 public 访问修饰符添加到 class IsolationEntryPoint,即在更改类签名之后,您的示例运行良好:

    public class IsolationEntryPoint : MarshalByRefObject
    {
        // [...]
    }
    

    【讨论】:

    • 哦,当然。 ReShaper 甚至对我大喊大叫,要给班级一个访问限定符。感谢您的帮助,为您 +100(一旦允许我这样做):)
    • 哟,有时 使用工具 胜过 使用源代码 甚至 ;) - 感谢慷慨的赏金,这使得解决这些难题更加困难值得:)
    【解决方案2】:

    我尝试了以下方法,它似乎有效。

    class Program
    {
    
        static void Main(string[] args)
        {
            SecurityPermission t = new SecurityPermission(SecurityPermissionFlag.Execution);
            t.Demand();
            IsolationEntryPoint x = new IsolationEntryPoint();
            x.Enter(AppDomain.CurrentDomain);
        }
    }
    
    
    class IsolationEntryPoint : MarshalByRefObject
    {
        // main is the original AppDomain with all the permissions 
        public void Enter(AppDomain main)
        {
            // these work correctly 
            Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine("Host: " + main.FriendlyName);
    
            // the exception is thrown here 
            main.DoCallBack(this.MyCallBack);
        }
    
        public void MyCallBack()
        {
            Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
        }
    }
    

    【讨论】:

    • 但是您是从默认的 AppDomain 执行它,默认情况下它确实具有反射权限。我正在从 AppDomain 执行它,没有权限,但执行。
    • 您可以尝试通过 appDomainObject.PermissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)) 显式向 AppDomain 添加自定义权限;
    • 这有什么帮助? AppDomain 有执行权限(为了安全起见,我不想添加反射权限)。我还发布了用于创建 AppDomain 的完整代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多