Search code examples
c#.net.net-assembly.net-5.net-4.8

Checking PublicKeyToken of a .NET 5 assembly from .NET Framework application


In our .NET Framework application we have a plugin mechanism which checks the PublicKeyToken before executing it to verify it is trusted. To this end we load the plugin temporarily as an assembly to fetch the PublicKeyToken.

Since we want to move to .NET 5, we compiled the plugins as such, but now we can't check for the PublicKeyToken anymore as we can't load a .NET 5 assembly in a .NET Framework process. Is there any way to check for the PublicKeyToken without loading the assembly?


Solution

  • The PublicKeyToken can be checked via P/Invoking a function in mscoree.dll:

        [DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
        static extern bool StrongNameTokenFromAssembly(string wszFilePath, out IntPtr ppbStrongNameToken, ref int pcbStrongNameToken);