You must have read my previous post on Difference Between PublicKey and PublicKeyToken. Now continue to that, where we need PublicKey?
Here I am giving one example to use PublicKey
We all know about “internal” keyword for access permission. Generally we are using internal keyword to restrict access for class, method or property from outside of assembly.
But there are certain case where we need to set some class or method accessible to only selected assemblies.
- In maintenance mode we are introducing some new assemblies
- We are providing add-ons on request.
To achieve this .Net framework provides, assembly attribute InternalsVisibleTo.
Below line should be added in AssemblyInfo.cs file of your project.
1 [assembly: InternalsVisibleTo("Your.Assembly.Name")]
Now in case of you have signed your assembly with key, it is mandatory for you to specify your public key in the attribute.
Syntax with PublicKey will be
1 [assembly: InternalsVisibleTo("Your.Assembly.Name, PublicKey=............Your.Public.Key............")]
Below are steps to generate your public key
1. Generate a strong name key for this example and store it in the file MySignedProject.snk:
sn -k MySignedProject.snk
2. Extract the public key from MySignedProject.snk and put it into MySignedProject.publickey:
sn -p MySignedProject.snk MySignedProject.publickey
3. Display the public key stored in the file MySignedProject.publickey:
sn -tp MySignedProject.publickey
Hope this will help you to provide guidance with multiple assembly intercommunication security.