C# 为什么要使用强命名程序集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2354129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:42:11  来源:igfitidea点击:

Why use strong named assemblies?

c#.net

提问by developer

What are the advantages of using strong named assemblies?

使用强命名程序集的优点是什么?

What are the things that can't be done with a normal assembly?

有哪些是普通装配无法做到的?

采纳答案by Kyle Rozendo

Let me list the benefits of strong naming your assembly first:

让我首先列出强命名程序集的好处:

  1. Strong naming your assembly allows you to include your assembly into the Global Assembly Cache(GAC). Thus it allows you to share it among multiple applications.

  2. Strong naming guarantees a unique name for that assembly. Thus no one else can use the same assembly name.

  3. Strong name protect the version lineage of an assembly. A strong name can ensure that no one is able to produce a subsequent version of your assembly. Application users are ensured that a version of the assembly they are loading come from the same publisher that created the version the application was built with.

  4. Strong named assemblies are signed with a digital signature. This protects the assembly from modification. Any tampering causes the verification process that occurs at assembly load time to fail. An exception is generated and the assembly is not loaded.

  1. 强命名程序集允许您将程序集包含到全局程序集缓存(GAC) 中。因此,它允许您在多个应用程序之间共享它。

  2. 强命名保证了该程序集的唯一名称。因此没有其他人可以使用相同的程序集名称。

  3. 强名称保护程序集的版本沿袭。强名称可以确保没有人能够生成程序集的后续版本。确保应用程序用户正在加载的程序集版本来自创建应用程序构建版本的同一发布者。

  4. 强命名程序集使用数字签名进行签名。这可以防止程序集被修改。任何篡改都会导致在程序集加载时发生的验证过程失败。生成异常并且未加载程序集。

More on strong naming from Microsoft is in Strong-Named Assemblies(MSDN).

有关 Microsoft 强命名的更多信息,请参见Strong-Named Assemblies( MSDN)。

回答by springy76

What are the things that can't be done with a normal assembly?

有哪些是普通装配无法做到的?

Since all the discussions that started with the rise of Nuget suggested to completely get rid of strong named assemblies my company tried that and came across a significant change of behavior when it comes to application settings:

由于随着 Nuget 的兴起开始的所有讨论都建议完全摆脱强命名程序集,因此我公司尝试了这一点,并且在应用程序设置方面遇到了重大的行为变化:

If you use the automatic app or user scoped application settings provided by VisualStudio (inheriting System.Configuration.ApplicationSettingsBase) then a strong named EXE will create exactly 1 directory inside %LOCALAPPDATA% named for example "YourApplication.exe_StrongName_kjsdfzsuzdfiuzgpoisdiufzsdouif" no matter where the EXE is located.

如果您使用 VisualStudio 提供的自动应用程序或用户范围的应用程序设置(继承 System.Configuration.ApplicationSettingsBase),那么无论 EXE 在哪里,一个强命名的 EXE 都会在 %LOCALAPPDATA% 内创建一个名为“YourApplication.exe_StrongName_kjsdfzsuzdfiuzgpoisdiufzsdouif”的目录位于。

But without the strong name the location (=path) of the EXE will be used to create a hash value which already differs between DEBUG and RELEASE build, creating many directories inside %LOCALAPPDATA% named like "YourApplication.exe_Url_dfg8778d6fs7g6d7f8g69sdf". This makes it unusable for ClickOnce deployments where the installation directory changes with every update.

但是如果没有强名称,EXE 的位置 (=path) 将用于创建一个哈希值,该值在 DEBUG 和 RELEASE 构建之间已经不同,在 %LOCALAPPDATA% 内创建许多目录,命名为“YourApplication.exe_Url_dfg8778d6fs7g6d7f8g69sdf”。这使得它无法用于 ClickOnce 部署,其中安装目录随每次更新而更改。

回答by Ricky Youssef

Just an example: I would like to give an answer doing more emphasis in the security. In the case we create assemblies with a source code that we don't want to be re-used for a third party but we want it to be testable, we can strongly sign an assembly and make the internals visible for only those assemblies with the same signature.

举个例子:我想给出一个更强调安全性的答案。如果我们创建的程序集的源代码不希望被第三方重复使用,但我们希望它是可测试的,我们可以对程序集进行强签名,并使内部结构仅对具有以下代码的程序集可见相同的签名。

回答by tomasat

I would like to add that without a strong name you cannot use binding redirects in config files.

我想补充一点,如果没有强名称,您将无法在配置文件中使用绑定重定向。

This will not work:

这将不起作用:

  <dependentAssembly>
    <assemblyIdentity name="MyAssembly.MyComponent" publicKeyToken="null" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>

You need to have a public key token

你需要有一个公钥令牌

  <dependentAssembly>
    <assemblyIdentity name="MyAssembly.MyComponent" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>