简体   繁体   中英

Prevent loading an assembly in c#

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.

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.

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

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.

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

Obfuscate it and keep documentation secret. Also include a key as in: http://www.codeguru.com/columns/experts/article.php/c4643

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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