2012-01-21 11 views
1

ノー権限でのAppDomainで作成するが、SecurityPermissionFlag.Executeインスタンスそのうち私は、このクラスを持っている:AppDomain.DoCallBackにはReflectionPermissionが必要ですか?

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、Culture =ニュートラル、PublicKeyToken = b77a5c561934e089' のアクセス許可が失敗しました。 AppDomain.DoCallBackの許可要件に関する

MSDNsays this

ReflectionPermission などType.InvokeMemberなどの機構を通じて遅延バインディング呼び出されたとき。

コールにはType.InvokeMemberのようなものは使用されていません。なぜ例外が表示されますか?

EDIT

明確にするために、ここで私は分離オブジェクトとの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つのコードは私の完全なアプリケーションです、何もありませんelse(例外が再現しやすいように)。あなたの助け

答えて

3

ソリューションは、実際には非常に単純です:あなたが好きなクラスの署名を変更した後class IsolationEntryPoint、すなわちに公共アクセス修飾子を追加するために逃したので、あなたのサンプルがうまく動作します:

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

ああコース。 ReShaperはクラスにアクセス修飾子を与えることを私に叫んでいます。助けてくれてありがとう、あなたのために+100をつけてください:)(0125)。 –

+0

あなたは時にはツール_トランプを使います.-祈りのソースを使ってください。) - 寛大な賞金をいただきありがとうございます。 –

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

しかし、あなたがしています既定でReflection特権を持つ既定のAppDomainから実行します。私はAppDomainから実行権限を持っていませんが、Executeです。 –

+0

appDomainObject.PermissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution))によってAppDomainにカスタムアクセス許可を明示的に追加することができます。 – Soundararajan

+0

これはどのように役立ちますか? AppDomainには実行権限があります(セキュリティ目的のために、私はリフレクション権限を追加したくありません)。 AppDomainの作成に使用する完全なコードも投稿しました。 –

関連する問題