这是我的 previous question 的后续内容,我想在加载程序集之前检查它的 StrongName(通过硬盘上的文件或通过字节数据)。确保它是我创建的。
使用Assembly.LoadFrom
或Assembly.Load
时是否存在需要考虑的安全风险,恶意代码是否可以通过将其加载到这些变量中来执行?我是否应该考虑将这些程序集加载到 AppDomain 中来读取它们?
这是我的其余代码:
Assembly dll = Assembly.LoadFrom("UnauthorisedPlugin.dll");
byte[] thisDllKey = Assembly.GetExecutingAssembly().GetName().GetPublicKey();
byte[] dllKey = dll.GetName().GetPublicKey();
if (Enumerable.SequenceEqual(thisDllKey, dllKey))
{
Type pluginType = dll.GetTypes().Single();
IPlugin unauthPlugin = (IPlugin)Activator.CreateInstance(pluginType);
Console.WriteLine(unauthPlugin.Run());
}
else
{
Console.WriteLine("The DLL is not authorised");
}
Console.ReadLine();
请您参考如下方法:
您可以通过以仅反射模式加载程序集来缓解部分问题:
The reflection-only load context allows you to examine assemblies compiled for other platforms or for other versions of the .NET Framework. Code loaded into this context can only be examined; it cannot be executed. This means that objects cannot be created, because constructors cannot be executed.
您可以使用 Assembly.ReflectionOnlyLoad()
和 Assembly.ReflectionOnlyLoadFrom()
来完成此操作。
请参阅此处了解更多信息 - http://msdn.microsoft.com/en-us/library/ms172331.aspx