简体   繁体   English

如何禁止引用.NET DLL类库

[英]How to forbid a .NET DLL class library to be referenced

我怎样才能禁止在其他解决方案中引用dll类库?

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. 你可以考虑加入StrongNameIdentityPermission你的类库相匹配的强有力的名字 想能够与使用它的程序。

Alternatively, you could explore using InternalsVisibleToAttribute , although it may require some design changes in your library code. 或者,您可以使用InternalsVisibleToAttribute探索,但可能需要对库代码进行一些设计更改。 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. 他们将无法添加引用,但它不会阻止某人通过Reflection或反汇编代码绕过它。 There are always ways around almost any security measure that you implement. 几乎所有安全措施都可以实现。

您可以使用名为StrongNameIdentityPermission的内容来阻止其他人引用您的库。

You can embed you DLL into your EXE via setting the "Build Action"-Property to "Embedded ressource". 您可以通过将“Build Action”--Property设置为“Embedded ressource”将DLL嵌入到EXE中。 Your DLL is not shipped as a single file and no one can use it. 您的DLL不是作为单个文件提供的,也没有人可以使用它。

You can make some dll class methods to fail if it is not used by your own main program. 如果您自己的主程序没有使用某些dll类方法,则可以使它们失败。

 Assembly main = System.Reflection.Assembly.GetExecutingAssembly();

 if (main.FullName != .....)
     throw new NotSupportedException();

or 要么

 if (IsSignedWith(main, myCompanysX509Certificate))
     throw new NotSupportedException();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM