我在主应用程序中运行宏程序集。宏不需要访问父组件。这是片段:在新应用程序域中加载程序集,需要父程序集完全可信
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是我的第一个片段。 –