Say I've written a .Net dll and want to distribute it with my application. How can I prevent any user with the savvy to install a copy of VS from adding a reference to this dll to their own applications?
相关问题
- Generic Generics in Managed C++
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
- How to Debug/Register a Permanent WMI Event Which
- Visual Studio 2019 - error MSB8020: The build tool
I seriously doubt this is possible. Even if it were, an assembly with full thrust can fake all evidence and pretend to be your program.
You cannot prevent someone from adding a reference to your dll. What you can do however is to make it hard for them to use it if they do add a reference.
Code Access Security (CAS) can be used to prevent unauthorised code for calling into an assembly, but this has an effect on the performance of calls, and is overkill for this purpose.
Another approach would be to mark everything in the dll as internal. If you have other assemblies that need access to the classes in the dll, make them friend assemblies using the InteralsVisibleTo attribute. This allows other assemblies to access to the internal classes of your dll. Tie this together with signed assemblies (to use with the
InternalsVisibleTo
definitions) and it should be enough.You could obfuscate the DLL (Tools -> Dotfuscator Community Edition) to make it unworthwile to reference your DLL.
There is no way to prevent a user from adding a reference. It's a passive action from the part of your DLL and there is nothing you can do to prevent it from occuring.
What you can do though, is make adding a reference a non-worthwhile operation. For instance, if all types in the DLL were internal or private, it take more than simply adding a reference for users to use the types in the DLL. Furthmore, obfuscating the DLL would make it even harder.
you might find "StrongNameIdentityPermission" class helpful
http://msdn.microsoft.com/en-us/library/system.security.permissions.strongnameidentitypermission.aspx
Although .NET does not allow true static linking, you may be able to fake it, packaging your dll and exe into one assembly. Here is a tool that can do this: ILMerge There may be others.