Another way:
use the StrongNameIdentityPermissionAttribute at the Assembly level to stop all assemblies from loading the signed assembly unless the loaders are signed with the same key AND / OR use the StrongNameIdentityPermissionAttribute at the Type or Member level to ensure that callers are signed by the same key.
Here is the assembly info for MyAssembly.dll:
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"..\..\..\EDS_Key.snk")]
[assembly: AssemblyKeyName("")]
[assembly: StrongNameIdentityPermissionAttribute(SecurityAction.RequestMinimum,
PublicKey=MyAssembly.Constants.PublicKey)]
Here is a type, defined in MyAssembly.dll:
using System;
namespace MyAssembly
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Constants
{
public const string PublicKey = "0024000004800000940000000602000000240000525341310004000001000100cbc86502ac31a6" +
"79f3b71e594bc5aca7cbde3cdcca074ee0c791a41cf3cc61e1a8770cb9d35e8b01b60885d5501e" +
"b7353440f7be071ee891c1e37064513e37aaf9da76930085b636795ef6ad1cf601203702083540" +
"a184f60537830ab059b471c5a0949b8d4f1512a30900a061364dca82fbd4c2e8df3f658b412ac0" +
"e40bdaa8";
private Constants()
{
//
// TODO: Add constructor logic here
//
}
}
}
And here is the assemblyinfo for an assembly called MySignedAssembly that references MyAssembly:
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"..\..\..\EDS_Key.snk")]
[assembly: AssemblyKeyName("")]
[assembly: StrongNameIdentityPermissionAttribute(SecurityAction.RequestMinimum,
PublicKey=MyAssembly.Constants.PublicKey)]
And lastly here is a type in MySignedAssembly that is protected from unsigned callers:
using System;
using System.Security;
using System.Security.Permissions;
namespace MySignedAssembly
{
/// <summary>
/// Summary description for Class1.
/// </summary>
[StrongNameIdentityPermissionAttribute(SecurityAction.LinkDemand,PublicKey=MyAssembly.Constants.PublicKey)]
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public static System.DateTime CurrentDate()
{
return System.DateTime.Now;
}
}
}
Hope this helps