Limiting Callers to a .NET Assembly

I've been asked the same question several times over the last couple of months, which
suggests that possibly the answer isn't as well known as I presumed. Since I have
to remind myself of the specifics of the answer each time, I figured I'd post both
question and answer to save us all time in the long run!

Q. How can I limit access to a .NET assembly that I've created? I've got
two separate assemblies A and B. A references B and
uses a number of instance methods on B, but I don't want any other assembly
to be able to access B. In effect, I'd like to make B "private"
to my application. Is there any way to achieve that with .NET?

A. There are probably a number of ways to achieve this, but the simplest
involves signing your calling assembly A with a unique public / private
key pair (use sn -k to achieve this). Once it's signed, you can use
the StrongNameIdentityPermission attribute on the callee assembly B to demand
that any callers are signed with a matching public key. If any other assembly tries
to call B that isn't signed with the same key, a SecurityException will be
thrown.

For more information on the StrongNameIdentityPermission attribute, see the appropriate
topic in the MSDN Library
. There's also a good walkthrough here.