[FxCop.设计规则]2. 程序集应该拥有有效的强命名

 

版权声明:

 

本文由timewolf完成,首发于CSDN ,作者保留版权。
未经许可,不得使用于任何商业用途。
欢迎转载,但请保持文章及版权声明完整。
如需联络请发邮件:karla9(AT)eyou(dot)com

原文引用:

Assemblies should have valid strong names

TypeName:

AssembliesShouldHaveValidStrongNames

CheckId:

CA2210

Category:

Microsoft.Design

Message Level:

CriticalError

Certainty:

95%

Breaking Change:

NonBreaking


Cause: An assembly is not signed with a strong name, the strong name could not be verified, or the strong name would not be valid without the current registry settings of the computer.

Rule Description

This rule retrieves and verifies the strong name of an assembly. A violation occurs if any of the following are true:

  • The assembly does not have a strong name.
  • The assembly was altered after signing.
  • The assembly is delay-signed.
  • The assembly was incorrectly signed, or signing failed.
  • The assembly requires registry settings to pass verification. For example, the Strong Name tool (Sn.exe) was used to skip verification for the assembly.

The strong name protects clients from unknowingly loading an assembly that has been tampered with. Assemblies without strong names should not be deployed outside of very limited scenarios. If you share or distribute assemblies that are not correctly signed, the assembly can be tampered with, the common language runtime might not load the assembly, or the user might have to disable verification on his or her computer. An assembly without a strong name suffers from the following drawbacks:

  • Its origins cannot be verified.
  • The common language runtime cannot warn users if the contents of the assembly have been altered.
  • It cannot be loaded into the global assembly cache.

Note that to load and analyze a delay-signed assembly, you must disable verification for the assembly.

How to Fix Violations

To fix a violation of this rule, use the Strong Name tool (sn.exe) to create a key file and sign the assembly with a strong name using one of the following procedures:

When to Exclude Messages

Only exclude a message from this rule if the assembly is used in an environment where tampering with the contents is not a concern.

引发原因:

1.  程序集没有使用强命名进行签名

2.  强命名不能被校验

3.  程序集的强命名依赖于当前计算机的设置才有效。

规则描述:

这个规则读取并校验一个程序集的强命名,下面任何一条都会引起这条规则校验失败:

1.  程序集没有进行强命名签名

2.  进行签名后,程序集被修改过。

3.  程序集被设置成延迟签名

4.  程序集签名失败

5.  程序集需要一定的注册表设置才能通过强命名校验

例如:通过强命名工具(Sn.exe)设置跳过对这个程序集的强命名校验

 

强命名保护用户不会使用一个被篡改的程序集。一个没有强命名的程序集只能被使用在非常小的范围内。如果你部署一个没有进行正确签名的程序集,程序集就有可能会被肆意篡改,以至于CLR可能会拒绝载入该程序集。在此情况下,用户就不得不将他的计算机设置为忽略强命名校验。

 

没有强命名的程序集有如下缺点:

1.  程序集的来源将不能被保证

2.  用户无从得知程序集是否被篡改过

3.  程序集不能被载入GAC(全局程序集缓存)

注意:如果需要分析一个延迟签名的程序集,必须禁用这条规则。

修复问题:

使用强名称工具(sn.exe)生成Key文件,并用这个文件签名程序集。你可以使用下面的一种方法签名这个程序集:

1.  使用.NET Framework SDK中的程序集连接工具(Al.exe)

2.  .NET Framework v1.0v1.1中,使用System.Reflection.AssemblyKeyFileAttributeSystem.Reflection.AssemblyKeyNameAttribute特性(Attribute)标记程序集。

3.  .NET Framework v2.0中,使用/keyfile或者/keycontainer编译指令。(在C++中,使用/KEYFILE or /KEYCONTAINER连接指令)

可否忽略:

只有当用户(或开发者)不在意程序集是否会被篡改的时候,可以忽略该规则引发的警告

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值