确保通过指定的程序集调用程序集
是否有任何内置功能可以确定是否从特定程序集调用程序集?
我有程序集A
,它引用程序集B
。程序集 A
公开 PowerShell cmdlet 并输出在 B
中找到的类型。 B
公开的类型中的某些方法和属性对程序集 A
中的类型感兴趣,但对 PowerShell 的使用者或任何尝试在 中加载类型的人不感兴趣>B
直接调用其中的方法。
我已经研究过InternalsVisibleToAttribute,但由于接口的使用,它需要大量的返工。我正在设计一个共享密钥系统,该系统后来被混淆,但看起来很笨重。
有什么方法可以确保 B
仅由 A
调用吗?
Is there any built in functionality to determine if an assembly is being called from a particular assembly?
I have assembly A
which references assembly B
. Assembly A
exposes PowerShell cmdlets and outputs types that are found within B
. Certain methods and properties with in types of exposed by B
are of interest to types in assembly A
but not of interest to consumers of PowerShell or anyone attempting to load types in B
directly and call methods within it.
I have looked into InternalsVisibleToAttribute
but it would require extensive rework because of the use of interfaces. I was devising a shared key system that would later be obfuscated but that seemed clunky.
Is there any way to ensure B
is called only by A
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在程序集上使用强名称键来执行此操作。
首先确保调用程序集(程序集 A)经过强名称签名(这可以在“签名”选项卡下的项目属性屏幕中完成)
以下代码将从调用程序集检索强名称密钥。
最简单的方法是使用相同的 StrongName 对两个程序集进行签名,然后验证 Assembly.GetCallingAssembly().Evidence 和 Assembly.GetExecutingAssembly().Evidence 是否由相同的 StrongName 进行签名。
在现有代码库上实现这可能不切实际,但请查看 LinFu AOP,您应该能够实现一个可以附加到需要检查有效调用者的类的属性。
You'd use a Strong Name key on your assemblies to do this.
First make sure the calling assembly (assembly A) is strong name signed (this can be done in the project properties screen under the Signing tab)
The following code will retrieve the strong name key from the calling assembly.
The easiest way would be to sign both assemblies with the same StrongName, then verify that Assembly.GetCallingAssembly().Evidence and Assembly.GetExecutingAssembly().Evidence are signed by the same StrongName.
This might be impractical to implement over an existing codebase, but take a look at LinFu AOP, you should be able to implement an attribute that can be attached to classes that need to be checked for a valid caller.
我认为
InternalsVisibleToAttribute
是最好的选择。 Assembly.GetCallingAssembly 的另一个选项检查i think
InternalsVisibleToAttribute
is best option. Another option checking of Assembly.GetCallingAssembly