确保通过指定的程序集调用程序集

发布于 2024-09-02 02:08:17 字数 423 浏览 3 评论 0原文

是否有任何内置功能可以确定是否从特定程序集调用程序集?

我有程序集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 技术交流群。

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

发布评论

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

评论(2

沉鱼一梦 2024-09-09 02:08:17

您可以在程序集上使用强名称键来执行此操作。

首先确保调用程序集(程序集 A)经过强名称签名(这可以在“签名”选项卡下的项目属性屏幕中完成)

以下代码将从调用程序集检索强名称密钥。

internal static StrongName GetStrongName(Evidence evidence)
{
    foreach (var e in evidence)
    {
        if (e is StrongName)
        {
            return (StrongName)e;
        }
    }
    throw new ArgumentException();
}

最简单的方法是使用相同的 StrongName 对两个程序集进行签名,然后验证 Assembly.GetCallingAssembly().Evidence 和 Assembly.GetExecutingAssembly().Evidence 是否由相同的 StrongName 进行签名。

var callerKey = GetStrongName(Assembly.GetCallingAssembly().Evidence).PublicKey;
var execKey = GetStrongName(Assembly.GetExecutingAssembly().Evidence).PublicKey;

if (callerKey != execKey)
{
    throw new UnauthorizedAccessException("The strong name of the calling assembly is invalid.");
}

在现有代码库上实现这可能不切实际,但请查看 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.

internal static StrongName GetStrongName(Evidence evidence)
{
    foreach (var e in evidence)
    {
        if (e is StrongName)
        {
            return (StrongName)e;
        }
    }
    throw new ArgumentException();
}

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.

var callerKey = GetStrongName(Assembly.GetCallingAssembly().Evidence).PublicKey;
var execKey = GetStrongName(Assembly.GetExecutingAssembly().Evidence).PublicKey;

if (callerKey != execKey)
{
    throw new UnauthorizedAccessException("The strong name of the calling assembly is invalid.");
}

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.

想你只要分分秒秒 2024-09-09 02:08:17

我认为 InternalsVisibleToAttribute 是最好的选择。 Assembly.GetCallingAssembly 的另一个选项检查

i think InternalsVisibleToAttribute is best option. Another option checking of Assembly.GetCallingAssembly

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