【发布时间】:2013-07-30 02:18:32
【问题描述】:
我继承的一个旧的 .NET Web 应用程序充满了奇怪的“程序集安全检查”:它检查 调用 程序集名称的公钥是否与公钥长度相同执行程序集的名称。
调用如下所示:
CheckAssembly(System.Reflection.Assembly.GetCallingAssembly(), System.Reflection.Assembly.GetExecutingAssembly(), this);
...那个方法是:
public static void CheckAssembly(Assembly callingAssembly, Assembly executingAssembly, object useObject)
{
byte[] assemblyPublicKey = executingAssembly.GetName().GetPublicKey();
byte[] callingPublicKey = callingAssembly.GetName().GetPublicKey();
if (callingPublicKey == null || assemblyPublicKey.Length != callingPublicKey.Length)
{
throw new SecurityException("The calling assembly does not have permission to use objects of type '" + useObject.GetType().FullName + "'");
}
for (int i = 0; i < assemblyPublicKey.Length; i++)
{
if (assemblyPublicKey[i] != callingPublicKey[i])
{
throw new SecurityException("The calling assembly does not have permission to use objects of type '" + useObject.GetType().FullName + "'");
}
}
}
我认为它正在检查它的程序集(DLL 文件)是否没有被交换或修改或任何东西。那是对的吗?如果不是,这段代码在做什么?关于为什么会写它的任何猜测?
我认为 .net 框架无论如何都会这样做,如果需要的话。对吧?
也许这是应用程序是 winforms 应用程序而不是 Web 应用程序时的旧代码?由于它是一个网络应用程序,我们可以完全控制服务器上的 DLL,因此没有安全风险,对吗?
(如果需要可以提供更多信息)。
【问题讨论】:
-
此代码的最佳位置是 thedailywtf.com
-
感谢@HansPassant,所以我认为它完全没用的假设是否正确?
-
我猜测第一次检查可能是为了避免进行第二次检查(我猜是更密集的)。我不知道它们多久会有不同的长度。
标签: .net reflection .net-assembly code-access-security