【问题标题】:Cannot find sn.exe to sign Assembly找不到 sn.exe 来签署程序集
【发布时间】:2010-12-04 21:15:50
【问题描述】:

我查看了C:\Program Files\Microsoft.NET,但看不到任何SN.exe 文件。

我安装了 .NET 3.5 运行时;这还不够吗?

【问题讨论】:

  • 如果您使用 Visual Studio 命令提示符,它应该可以开箱即用

标签: c# msbuild sn.exe


【解决方案1】:

您需要安装 Windows SDK 6.0a,而不仅仅是运行时。

如果你已经安装了VS2008,你会发现它已经安装好了,sn.exe会在这里:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe

否则,如果你没有安装VS2008,你可以单独下载SDKhere

SDK 中没有 sn.exe 文件。 SDK的当前版本是6.1,也许他们在这个版本中删除了sn.exe。

【讨论】:

  • 64位版本呢?
  • 对于使用 VS2010 的 Windows 7 64 位的我来说,sn.exe 的路径是 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe
  • 链接已损坏。
【解决方案2】:
  • 打开命令提示符
  • 输入cd \
  • 输入dir /s sn.exe
  • 你会得到类似的输出

    Volume in drive C has no label.

    Volume Serial Number is XXXX-XXXX.

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin的目录

11/07/2007  12:01 PM            95,728 sn.exe
              1 File(s)         95,728 bytes

你找到了目录:)
如果没有,您的系统中没有sn.exe。然后安装 SDK。

【讨论】:

    【解决方案3】:

    我相信你有你的理由——而且在很多情况下SN.exe 是不可避免的和/或适当的(延迟签名)。 (我已经 +1 了 Q 和接受的 A 并且没有以任何方式质疑它们的优点,所以如果它不适用于您的情况,请忽略这一点)

    请注意,SN.exe 在实践中很少需要 - 驱动编译器的Microft.<lang>.targets 中的接线[和AL.exe 等] 所有[有效] 都考虑了 .proj 文件中的SignAssembly 标志,并且有条件地将密钥传递给编译器等,因此它可以通过内联程序集的一次触摸完成所有工作(主要是出于性能原因)。

    此逻辑还处理 .snk.pfx 密钥之间的区别(它们受密码保护并被秘密存储到密钥容器中)。根据哪种形式,在运行时目录中会出现由Microsoft.Common.targets 解析的KeyContainerNameKeyOriginatorFile 属性 - 搜索ResolveKeySource

    如果您需要执行SN 的原因是因为您刚刚重写了一个程序集,那么通常应该使用相同的模式,即Mono.Cecil 和类似 PostSharp 的工具(我假设,未确认)通常也需要可以使用相同的参数和/或进行内联签名。


    Microsoft.Common.targets 摘录

    <Target Name="ResolveKeySource" 
      Condition="$(SignManifests) == 'true' or $(SignAssembly) == 'true'">
    
      <ResolveKeySource ...
        KeyFile="$(AssemblyOriginatorKeyFile)"
        CertificateFile="$(ManifestKeyFile)"
        SuppressAutoClosePasswordPrompt="$(BuildingInsideVisualStudio)">
          <Output TaskParameter="ResolvedKeyFile" PropertyName="KeyOriginatorFile" ..."/>
          <Output TaskParameter="ResolvedKeyContainer" PropertyName="KeyContainerName" ... "/>
    

    Microsoft.CSharp.targets 摘录

        <Csc  ...
              KeyContainer="$(KeyContainerName)"
              KeyFile="$(KeyOriginatorFile)" />
    

    为了完整起见,以下是如何以编程方式推断与您正在编译的目标相关的 SDK 路径(在 4.0 上测试,但同样的方法可以一直追溯到 2.0,即Microsoft.Common.targets 已处理此数据一段时间):

    <Target Name="ResolveSNToolPath" Condition=" 'true' == '$(SignAssembly)' ">
        <PropertyGroup>
          <_SdkToolsBinDir Condition=" '' == '$(_SdkToolsBinDir)' ">$(TargetFrameworkSDKToolsDirectory)</_SdkToolsBinDir>
          <SNToolPath Condition=" '' == '$(SNToolPath)' ">$(_SdkToolsBinDir)SN.exe</SNToolPath>
        </PropertyGroup>
        <Error Condition=" 'true' == '$(SignAssembly)' AND !EXISTS( '$(SNToolPath)' )"
          Text="In order to resign the assembly, this package requires access to the SN.EXE tool from the Windows Platform SDK, which was not found.
    
    The location derived was &quot;$(SNToolPath)&quot;.
    
    Please either:
    1) supply a correct path to your SDK Tools bin directory containing SN.EXE by setting %24(_SdkToolsBinDir) or %24(TargetFrameworkSDKToolsDirectory)
    OR
    2) supply a correct complete path to your SN.EXE signing tool by setting %24(SNToolPath)" />
      </Target>
    

    为了完整起见,以下是您如何利用此过程的输出来运行 SN.exe

    <Target Name="ResignMyAssembly" Condition="$(SignAssembly) == 'true'">
      <Exec Condition=" '$(KeyContainerName)' != '' " 
        Command="&quot;$(SNToolPath)&quot; -Rca &quot;@(MyAssembly)&quot; &quot;$(KeyContainerName)&quot; " />
      <Exec Condition=" '$(KeyContainerName)' == '' " 
        Command="&quot;$(SlpsSdkProtectSnTool)&quot; -Ra &quot;@(MyAssembly)&quot; &quot;$(KeyOriginatorFile)&quot; " />
    

    【讨论】:

      【解决方案4】:

      它是 SDK 的一部分(.NET,或者现在的 Windows SDK

      【讨论】:

        【解决方案5】:

        对于 VS2017 路径更改为: C:\Program Files (x86)\Microsoft SDKs\Windows\vX\bin\NETFX X.X.X Tools\.

        【讨论】:

          【解决方案6】:

          很简单:

          在windows下(根据.net framework版本\B8.1A..的路径变化),转到=>

          C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 工具

          编写您的 sn.exe 命令:

          sn -i D:\XX\MYProject.UI.api\MYProject.Gateway\my_certificate.pfx VS_KEY_AD6FD8AFB39B6C43
          

          如果它是受密码保护的,那么它会希望 pwd 把它写下来

          【讨论】:

            【解决方案7】:

            不,看来您需要 SDK :(

            仅供参考,运行时本身不会在 C:\Program Files\Microsoft.NET 下——它的所有文件都在 [仅] 在 C:\Windows\Microsoft.NET\vXXXXXX\ 下运行

            【讨论】:

            • 不,那是运行时文件夹。其中包含一些工具,但不包含 sn。
            • @HenkHolterman(和 OP)已编辑答案以使其更清晰。我认为a)有一个重要的一点是其他答案没有涵盖b)我们都应该删除我们的cmets,因为它们对答案没有意义,因为它是c)如果有协议,downvoter应该删除downvote。至于我怎么会记得删除这个...... :)
            【解决方案8】:

            对于 VS2019,路径是 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools\x64\sn.exe

            现在我仍然无法使用 VS 命令提示符。它向我显示消息,例如

            ** Visual Studio 2017 开发人员命令提示 v15.8.9 ** 版权所有 (c) 2017 Microsoft Corporation

            [vcvarsall.bat] 环境初始化为:'x64'

            C:\Program Files (x86)\Microsoft Visual Studio\2017\Community>其中 sn.exe 信息:找不到给定模式的文件。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-12-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多