我在主應用程序中運行宏程序集。宏不需要訪問父組件。這是片段:在新應用程序域中加載程序集,需要父程序集完全可信
Assembly ParentAssembly
{
class c1
{
void RunMacro()
{
System.Security.PermissionSet PS = new System.Security.PermissionSet(PermissionState.None);
PS.AddPermission(new SOME_PERMISSIONS....);
AppDomainSetup ADS = new AppDomainSetup();
ADS.ApplicationBase = "c:";
AppDomain domain = AppDomain.CreateDomain(SomeName, null, ADS, PS);
System.Runtime.Remoting.ObjectHandle handle = Activator.CreateInstanceFrom(domain, typeof(Sandboxer2).Assembly.ManifestModule.FullyQualifiedName, typeof(Sandboxer2).FullName);
Sandboxer2 m = (Sandboxer2)handle.Unwrap();
m.Execute();
}
}
}
我收到此異常:
嘗試通過安全透明方法 'SandBoxer.Sandboxer2.Execute()' 來訪問安全臨界方法 「System.AppDomain.add_AssemblyResolve (System.ResolveEventHandler)' 失敗。
組件'Parent Assembly full name ...'部分是可信的,其中 導致CLR使其完全安全透明,而不管 組件本身中的任何透明度註釋。爲了對 訪問安全關鍵代碼,此程序集必須完全信任。
我的問題:
有什麼辦法避免小孩議會裝載親組件?
在我的代碼的第二行中,什麼權限可以解決這個問題?
有一些程序集將在運行時由SandBoxer的AssemblyResolve事件加載。程序集從數據庫中以二進制數組或從GAC加載。他們不完全信任。我通過在第二行代碼中添加權限對象來控制他們的行爲。是否有特殊權限需要添加才能讓它們僅作爲部分受信任的程序集加載?
我覺得一切都可以通過添加安全權限一樣的代碼第二行來完成,如果我誤解的概念,我將不勝感激加以引導。
EDIT1: 父組件是創建SandBoxr實例並運行它的主應用程序的彙編。請看看SandBoxer2類和它的執行方法:
public class Sandboxer2 : MarshalByRefObject
{
public void Execute()
{
AppDomain ad = AppDomain.CurrentDomain;
ad.AssemblyResolve += MyHandler;
.
.
.
}
}
裏面Execute方法,廣告實例化後,我用ad.GetAssemblies(),這是已加載的所有組件的列表。 2號行從砂箱執行的最初階段開始持有ParentAssembly。
- [0] {mscorlib程序,版本= 4.0.0.0,文化=中性公鑰= b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [1] {系統。幅,版本= 4.0.0.0,文化=中性公鑰= b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [2] {ParentAssembly,版本= 1.0.0.0,文化=中性公鑰= null} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [3] {System.Data,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089} System.Reflection。組件 {System.Reflection.RuntimeAssembly}
- [4] {系統,版本= 4.0.0.0,文化=中性公鑰= b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [5 ] {MacroBase_IO,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [6] {System.Core,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [7] {System.Configuration,Version = 4.0.0.0,Culture = neutral,Pu blicKeyToken = b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [8] {的System.Xml,版本= 4.0.0.0,文化=中性公鑰= b77a5c561934e089} System.Reflection.Assembly {系統.Reflection.RuntimeAssembly}
你打電話給「父母」的是什麼組合? (顯然不是一個擁有'SandBoxer2',但沒有提到其他的組件)。使用特定的名稱可能會更好(即使與您在實際代碼中使用的不完全相同)。 –
我編輯了我的問題。 ParentAssembly是我的第一個片段。 –