[英]Load Assembly in new Appdomain, needs parent assembly to be fully trusted
我在主应用程序中运行宏程序集。 宏不需要访问父程序集。 这是代码段:
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)'失败。
程序集“父程序集全名...”是部分受信任的,这使CLR使其完全安全透明,而不管程序集本身中是否有任何透明性注释。 为了访问安全关键代码,必须完全信任此程序集。
我的问题:
有什么方法可以避免在子程序集中加载父程序集?
在我的代码的第二行中,哪些权限可以解决问题?
在运行时,SandBoxer的AssemblyResolve事件将加载一些程序集。 程序集从数据库以二进制数组或GAC的形式加载。 他们不被完全信任。 我通过在第二行代码中添加的权限对象来控制其行为。 我是否需要添加特殊权限以使它们仅作为部分受信任的程序集加载?
我认为可以通过添加安全权限(如第二行代码)来完成所有工作。如果我误解了这一概念,我将不胜感激。
编辑1:父程序集是创建SandBoxr实例并运行它的主应用程序的程序集。 请看一下SandBoxer2类及其Execute方法:
public class Sandboxer2 : MarshalByRefObject
{
public void Execute()
{
AppDomain ad = AppDomain.CurrentDomain;
ad.AssemblyResolve += MyHandler;
.
.
.
}
}
在Execute方法内部,实例化ad之后,我使用了ad.GetAssemblies(),这是已加载的所有程序集的列表。 第2行从沙盒程序执行的一开始就保存了ParentAssembly。
- [0] {mscorlib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [1] {System.Web,版本= 4.0.0.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [2] {ParentAssembly,版本= 1.0.0.0,文化=中性,PublicKeyToken =空} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [3] {System.Data,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [4] {System,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [5] {MacroBase_IO,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [6] {System.Core,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [7] {System.Configuration,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
- [8] {System.Xml,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089} System.Reflection.Assembly {System.Reflection.RuntimeAssembly}
回答问题1和2:
沙盒程序必须位于单独的程序集(另一个DLL)中,并且此单独的程序集必须用密钥签名。 然后,将不会自动加载主应用程序,并且不会引发此异常。
编辑:
1-通过键签名是通过“程序集签名”选项卡的属性完成的。
2-此示例有助于理解如何将程序集定义为完全信任,并为Sandbox引入强名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.