防止在 C# 中加载程序集
我想阻止从另一个应用程序加载程序集。这样它只能从我的应用程序加载。
现在我正在使用 Assambly.LoadFrom 来加载程序集。
I want to prevent an assembly to be loaded from another application. So that it can be loaded from my application only.
Rigth now I'm using Assambly.LoadFrom
to load the assembly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最终,没有。听起来您正在部署一个 dll,但您希望保留对其使用方式的唯一控制。这只是一场军备竞赛;最终,如果有人真的、真的想要(滥用)使用它,他们就可以。即使这意味着反汇编、反混淆和解除您添加的任何预防性代码。
阻止这种情况的唯一方法是:不要给他们。考虑使用网络服务来实现某些功能。那么他们就没有集会。
Ultimately, no. It sounds like you are deploying a dll, but you want to retain sole control over how it is used. That is just an arms race; ultimately if somebody really, really wants to (ab)use it, they can. Even if that means disassembling, de-obfuscating, and disarming any preventative code you have added.
The only way to block that: don't give it to them. Consider using a web-service for some functions. Then they don't have the assembly.
您可以尝试在“受保护”程序集中的某处验证条目程序集 (
Assembly.GetEntryAssembly
)。You can try to validating entry assembly (
Assembly.GetEntryAssembly
) somewhere in your 'protected' assembly.您无法通过技术方法阻止人们使用您的 DLL。您可以尝试使其变得更加困难,但足够熟练的用户将能够修改程序集以便他们可以使用它。
你可以尝试走法律途径。您可以在许可协议中包含一个条款,禁止人们在其他应用程序中使用您的程序集。但这不会阻止所有人。
You can't stop people from using your DLL using technological approach. You can try to make it more difficult but a sufficiently skilled user will be able to modify the assembly so that they can use it.
You could try a legal approach. You can include a clause in your license agreement which disallows people from using your assembly in another application. But it won't stop everyone.
您可以使用
InternalsVisibleToAttribute
< /a> 在您的程序集中并将您的应用程序程序集指定为“朋友”。这将阻止其他程序集使用被标记为内部
的类型,但将允许您的应用程序仍然使用这些类型。来自 MSDN:
这并不是说这不会阻止任何人加载您的程序集,它只是阻止他们使用程序集中的内部类型(至少不是没有一些重大努力,最终任何人都可以反汇编你的代码并以这种方式使用它)。
You could use the
InternalsVisibleToAttribute
in your assembly and specify your application assembly as a "friend". This will prevent other assemblies of using types provided they are marked asinternal
but will allow your application to still use those types.From MSDN:
Not that this does not prevent anyone from loading your assembly, it just prevents them from using the internal types within the assembly (at least not without some major effort, in the end anyone can just disassemble your code and use it that way).
对其进行混淆并保密文档。还包括一个密钥,如下所示:
http://www.codeguru.com/columns/experts/article.php/c4643
Obfuscate it and keep documentation secret. Also include a key as in:
http://www.codeguru.com/columns/experts/article.php/c4643