1

私はこのクラスを持っています。そのインスタンスの 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、Version=4.0.0.0、Culture=neutral、PublicKeyToken=b77a5c561934e089' のアクセス許可の要求に失敗しました。

MSDNは、AppDomain.DoCallBack のアクセス許可要件について次のように述べています。

Type.InvokeMember などのメカニズムを介して遅延バインディングで呼び出された場合の ReflectionPermission。

呼び出しは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);
    }

これらの 2 つのコードは私の完全なアプリケーションであり、他には何もありません (したがって、例外は簡単に再現できるはずです)。

ご協力いただきありがとうございます

4

2 に答える 2

3

解決策は実際には非常に単純です。パブリックアクセス修飾子をに追加できなかったclass IsolationEntryPoint、つまり、クラスの署名を次のように変更した後、サンプルは正常に実行されます。

public class IsolationEntryPoint : MarshalByRefObject
{
    // [...]
}
于 2012-01-25T12:24:20.153 に答える
0

以下を試してみましたが、うまくいくようです。

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);
    }
}
于 2012-01-21T19:29:34.223 に答える