How to forbid a .NET DLL class library to be referenced

前端 未结 4 1560
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 11:42

How can I forbid dll class library to be referenced in other solutions?

相关标签:
4条回答
  • 2021-01-14 12:12

    You could look into adding a StrongNameIdentityPermission to your class library that matches the strong name of the program you do want to be able to use it with.

    Alternatively, you could explore using InternalsVisibleToAttribute, although it may require some design changes in your library code. This should work as long as neither assembly is signed, or both are signed with a strong name. The argument specified on the attribute should match the public key and the name of the assembly that you want to be able to access its internal members.

    But really, this will only stop someone who isn't trying very hard to use your library. They won't be able to add a reference, but it doesn't prevent someone from bypassing it through Reflection or disassembling your code. There are always ways around almost any security measure that you implement.

    0 讨论(0)
  • 2021-01-14 12:13

    You can make some dll class methods to fail if it is not used by your own main program.

     Assembly main = System.Reflection.Assembly.GetExecutingAssembly();
    
     if (main.FullName != .....)
         throw new NotSupportedException();
    

    or

     if (IsSignedWith(main, myCompanysX509Certificate))
         throw new NotSupportedException();
    
    0 讨论(0)
  • 2021-01-14 12:17

    You can embed you DLL into your EXE via setting the "Build Action"-Property to "Embedded ressource". Your DLL is not shipped as a single file and no one can use it.

    0 讨论(0)
  • 2021-01-14 12:26

    You can use something called as StrongNameIdentityPermission to prevent others from referencing your library.

    0 讨论(0)
提交回复
热议问题