检查程序集的身份?可能是因为它的名字很响亮?

发布于 2024-11-26 16:56:45 字数 121 浏览 2 评论 0 原文

在我的一个应用程序中,我必须将我自己包含的插件与其他人的插件区分开来。
它们各自使用不同的密钥进行签名,主机应用程序也是如此。

有什么方法可以区分我的程序集和其他人的程序集吗? (可能需要签名密钥的帮助)

In one of my apps I must distinguish my own included addins from others' addins.
They are signed with different keys each and so is the host app.

Is there any way to distinguish my assemblies from others' assemblies? (Possibly with the help of the signing key)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

彩扇题诗 2024-12-03 16:56:45

您可以根据容器程序集中存储的列表检查加载的程序集公钥令牌。

http://blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named- assembly.aspx

或者只是简单地存储程序集的全名,如下所示:

var assembly = typeof (string).Assembly;

var myAssemblies = new HashSet<string>
{
 "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
};

Assert.IsTrue(myAssemblies.Contains(assembly.FullName));

您还可以使用 GetPublicKeyToken 和 BitConverter 检查令牌

var assembly = typeof(string).Assembly;

var token = BitConverter.ToString(assembly.GetName().GetPublicKeyToken()).Replace("-","").ToLowerInvariant();

var expectedToken = "b77a5c561934e089";

Assert.AreEqual(expectedToken, token);

You can check the loaded assembly public key token against the list that is stored in the container assembly.

http://blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named-assembly.aspx

Or just simply store the full names of the assemblies like this:

var assembly = typeof (string).Assembly;

var myAssemblies = new HashSet<string>
{
 "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
};

Assert.IsTrue(myAssemblies.Contains(assembly.FullName));

Also you can check egainst the tokens, using GetPublicKeyToken and BitConverter

var assembly = typeof(string).Assembly;

var token = BitConverter.ToString(assembly.GetName().GetPublicKeyToken()).Replace("-","").ToLowerInvariant();

var expectedToken = "b77a5c561934e089";

Assert.AreEqual(expectedToken, token);
难以启齿的温柔 2024-12-03 16:56:45

如果您的所有程序集都已签名,您可以使用 GetPublicKey()GetPublicKeyToken()

 Assembly a =...
 a.GetName().GetPublicKeyToken();

 Assembly a =...
 a.GetName().GetPublicKey();

这比保留 GUID 列表更方便,并且也适用于将来的程序集。

If all your assemblies are signed you can use either GetPublicKey() or GetPublicKeyToken()

 Assembly a =...
 a.GetName().GetPublicKeyToken();

 Assembly a =...
 a.GetName().GetPublicKey();

and would be more convenient than keeping list of GUID and would work with future assemblies too.

独夜无伴 2024-12-03 16:56:45

您可以通过检查强名称签名来完成此操作。请参阅检查有效的强名称签名了解如何执行此操作。请注意,本文提到将 P/invoke 与 StrongNameSignatureVerificationEx 结合使用,但如果您使用的是 .NET 4.0 或更高版本,则应替换 ICLRStrongName:: StrongNameSignatureVerificationEx

请参阅公钥和公钥令牌了解公钥令牌的实际含义。鉴于它只是公钥的哈希值,因此很容易复制或提取,然后注入到不受信任的程序集中,因此它不是用于验证程序集的安全机制。

You can accomplish this by checking the strong name signature. See Checking For A Valid Strong Name Signature for how to this. Note that the article refers to using P/invoke with StrongNameSignatureVerificationEx, but if you are using .NET 4.0 or above you should substitute ICLRStrongName::StrongNameSignatureVerificationEx.

See Public Keys and Public Key Tokens for what a public key token actually is. Given that it is only a hash of the public key, and therefore easily duplicated or extracted and then injected into an untrusted assembly, it isn't a secure mechanism for validating an assembly.

紫竹語嫣☆ 2024-12-03 16:56:45

一个不错的片段:

// Assembly is a System.Reflection.Assembly
public string GetAssemblyGUID(Assembly assembly)
{
    object[] objects = assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
    if (objects.Length > 0)
        return ((System.Runtime.InteropServices.GuidAttribute)objects[0]).Value;
    else
        return null;
}

这将检索指定程序集的 GUID。您可以在项目属性中设置程序集的 GUID。对于类库和 PE 来说也是如此。

A little nice snippet:

// Assembly is a System.Reflection.Assembly
public string GetAssemblyGUID(Assembly assembly)
{
    object[] objects = assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
    if (objects.Length > 0)
        return ((System.Runtime.InteropServices.GuidAttribute)objects[0]).Value;
    else
        return null;
}

This will retrieve the GUID of the specified assembly. You can set your assembly's GUID in Project Properties. This holds true for class libraries and PEs.

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