AppDomain.DoCallBack 需要 ReflectionPermission?

发布于 2024-12-28 11:56:11 字数 2436 浏览 1 评论 0原文

我有这个类,我在 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 说关于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);
    }

这些两块代码是我的完整应用程序,没有其他内容(因此异常应该很容易重现)。

感谢您的帮助

I have this class, instance of which I create in an AppDomain with no permissions but 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);
    }
}

The odd thing is that I get SecurityException in the DoCallback line saying:

Request for the permission of type
'System.Security.Permissions.ReflectionPermission, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed.

MSDNsays this about permission requirements of AppDomain.DoCallBack:

ReflectionPermission when invoked late-bound through mechanisms such
as Type.InvokeMember.

The call is not using anything like Type.InvokeMember, why am I getting the exception?

EDIT:

For clarity, here is the code I use to create the AppDomain with the isolation object:

    [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);
    }

These two pieces code are my full application, there is nothing else (so the exception should be easy to reproduce).

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2025-01-04 11:56:11

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

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

The solution is actually quite simple: You missed to add the public access modifier to class IsolationEntryPoint, i.e after changing the class signature like so your sample runs just fine:

public class IsolationEntryPoint : MarshalByRefObject
{
    // [...]
}
快乐很简单 2025-01-04 11:56:11

我尝试了下面的方法,似乎有效。

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);
    }
}

I tried the below and it seems to work.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文