views:

105

answers:

2

Is there any built in functionality to determine if an assembly is being called from a particular assembly?

I have assembly A which references assembly B. Assembly A exposes PowerShell cmdlets and outputs types that are found within B. Certain methods and properties with in types of exposed by B are of interest to types in assembly A but not of interest to consumers of PowerShell or anyone attempting to load types in B directly and call methods within it.

I have looked into InternalsVisibleToAttribute but it would require extensive rework because of the use of interfaces. I was devising a shared key system that would later be obfuscated but that seemed clunky.

Is there any way to ensure B is called only by A?

+1  A: 

i think InternalsVisibleToAttribute is best option. Another option checking of Assembly.GetCallingAssembly

Andrey
+3  A: 

You'd use a Strong Name key on your assemblies to do this.

First make sure the calling assembly (assembly A) is strong name signed (this can be done in the project properties screen under the Signing tab)

The following code will retrieve the strong name key from the calling assembly.

internal static StrongName GetStrongName(Evidence evidence)
{
    foreach (var e in evidence)
    {
        if (e is StrongName)
        {
            return (StrongName)e;
        }
    }
    throw new ArgumentException();
}

The easiest way would be to sign both assemblies with the same StrongName, then verify that Assembly.GetCallingAssembly().Evidence and Assembly.GetExecutingAssembly().Evidence are signed by the same StrongName.

var callerKey = GetStrongName(Assembly.GetCallingAssembly().Evidence).PublicKey;
var execKey = GetStrongName(Assembly.GetExecutingAssembly().Evidence).PublicKey;

if (callerKey != execKey)
{
    throw new UnauthorizedAccessException("The strong name of the calling assembly is invalid.");
}

This might be impractical to implement over an existing codebase, but take a look at LinFu AOP, you should be able to implement an attribute that can be attached to classes that need to be checked for a valid caller.

Kevin McKelvin