防止在 C# 中加载程序集

发布于 2024-10-17 23:41:12 字数 82 浏览 6 评论 0原文

我想阻止从另一个应用程序加载程序集。这样它只能从我的应用程序加载。

现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜灵血窟げ 2024-10-24 23:41:12

最终,没有。听起来您正在部署一个 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.

长梦不多时 2024-10-24 23:41:12

您可以尝试在“受保护”程序集中的某处验证条目程序集 (Assembly.GetEntryAssembly)。

You can try to validating entry assembly (Assembly.GetEntryAssembly) somewhere in your 'protected' assembly.

南汐寒笙箫 2024-10-24 23:41:12

您无法通过技术方法阻止人们使用您的 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.

在梵高的星空下 2024-10-24 23:41:12

您可以使用 InternalsVisibleToAttribute< /a> 在您的程序集中并将您的应用程序程序集指定为“朋友”。这将阻止其他程序集使用被标记为内部的类型,但将允许您的应用程序仍然使用这些类型。

来自 MSDN:

通常,类型和成员具有
内部作用域(C# 中)和友元
范围(在 Visual Basic 中)可见
仅在他们所在的议会中
定义的。这
InternalsVisibleToAttribute 属性
使它们对类型也可见
在指定的程序集中。

该属性应用于
装配级别。这意味着它可以
包含在 a 的开头
源代码文件,或者它可以是
包含在 AssemblyInfo 文件中
视觉工作室项目。您可以使用
用于指定单个程序集的属性
可以访问内部类型和
本届议会成员。到
使程序集的内部类型和
成员对其他成员可见
程序集,您必须包括
单独的InternalsVisibleToAttribute
每个程序集的属性。

这并不是说这不会阻止任何人加载您的程序集,它只是阻止他们使用程序集中的内部类型(至少不是没有一些重大努力,最终任何人都可以反汇编你的代码并以这种方式使用它)。

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 as internal but will allow your application to still use those types.

From MSDN:

Ordinarily, types and members with
internal scope (in C#) and friend
scope (in Visual Basic) are visible
only in the assembly in which they are
defined. The
InternalsVisibleToAttribute attribute
makes them also visible to the types
in a specified assembly.

The attribute is applied at the
assembly level. This means that it can
be included at the beginning of a
source code file, or it can be
included in the AssemblyInfo file in a
Visual Studio project. You can use the
attribute to specify a single assembly
that can access the internal types and
members of the current assembly. To
make the assembly's internal types and
members visible to additional
assemblies, you must include a
separate InternalsVisibleToAttribute
attribute for each assembly.

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).

却一份温柔 2024-10-24 23:41:12

对其进行混淆并保密文档。还包括一个密钥,如下所示:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文