Postsharp 抛出 System.Runtime.InteropServices.COMException (0x8013141C):未找到强名称密钥容器。 (HRESULT 异常:0x8013141C)

Postsharp throws System.Runtime.InteropServices.COMException (0x8013141C): Strong name key container not found. (Exception from HRESULT: 0x8013141C)

为将来需要解决相同问题的任何人添加问题

环境:Visual Studio 2017 年或 2019 年
Postsharp 对程序集进行签名时出现构建错误

MSBuild postsharptest.csproj /T:Rebuild /P:PostSharpTrace=StrongName /V:D /P:PostSharpHost=Native > postsharp.log

C:\ProgramData\PostSharp.0.41\bin.Release\postsharp.4.0-x86.exe obj\Debug\Before-PostSharp\PostsharpTest.exe /X:default /NoLogo /P:Output=C:\PostsharpTest\obj\Debug\PostsharpTest.exe /P:ReferenceDirectory=C:\PostsharpTest /P:Configuration=Debug /P:Platform=AnyCPU "/P:SearchPath=bin\Debug\|obj\Debug\|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\ " /P:IntermediateDirectory=obj\Debug\PostSharp /P:CleanIntermediate=False /P:MSBuildProjectFullPath=C:\PostsharpTest\postsharptest.csproj /P:SignAssembly=true /P:PrivateKeyLocation=Thycotic.snk /P:PrivateKeyContainer= /P:DelaySign= "/P:ResolvedReferences=C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll|C:\PostsharpTest\packages\PostSharp.4.0.41\lib\net35-client\PostSharp.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.DataSetExtensions.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll|C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.Linq.dll" /P:SymbolFile=bin\Debug\PostsharpTest.pssym /P:WriteWeavingSymbols=True /P:ConstraintVerificationEnabled=True /P:RuntimeVerificationEnabled=True /P:TargetFrameworkVersion=v4.5 /P:TargetFrameworkIdentifier=.NETFramework /P:TargetFrameworkProfile= "/P:TargetFrameworkMoniker=.NETFramework,v4.5" "/P:NativeFrameworkMoniker=.NETFramework,v4.5.1" /P:BindingMapsDirectory=C:\ProgramData\PostSharp.0.41\BindingMaps /P:PostSharpLibraryProfile= /P:AspectProviders= "/P:Language=C#" /T:StrongName /config C:\ProgramData\PostSharp.0.41\bin.Release\postsharp.4.0-x86.exe.config 
C:\PostsharpTest\postsharptest.csproj : warning PS0131: The project 'ModuleDeclaration PostsharpTest.exe' does not contain any aspect or other transformation. You can disable PostSharp for this project by editing the project properties in Visual Studio. Remember to enable PostSharp back if you add an aspect to this project.
C:\PostsharpTest\postsharptest.csproj : error PS0099: Unhandled exception (4.0.41.0, 32 bit, CLR 4.5, Release): System.Runtime.InteropServices.COMException (0x8013141C): Strong name key container not found. (Exception from HRESULT: 0x8013141C)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^nIsIyZ7TTJqY.GetKeyInfo(^8gP93Xsl\+Imn _0)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^Up6wxUIUnN6a.^3w8852HL()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^Up6wxUIUnN6a.^VfvrABjR()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^Up6wxUIUnN6a.^SkiT()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^Up6wxUIUnN6a.^8KD6D2SV(String _0)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at ^cyC/TYSlPGB/.^wvPm(^RtksFin57NJ8 _0, ^8gP93Xsl\+Imn _1)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at PostSharp.Sdk.Extensibility.Tasks.CompileTask.Execute()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at PostSharp.Sdk.Extensibility.Project.ExecutePhase(String phase)
C:\PostsharpTest\postsharptest.csproj : error PS0099: at PostSharp.Sdk.Extensibility.Project.Execute()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at PostSharp.Hosting.PostSharpObject.ExecuteProjects()
C:\PostsharpTest\postsharptest.csproj : error PS0099: at PostSharp.Hosting.PostSharpObject.InvokeProject(ProjectInvocation projectInvocation).

Properties | Signing | Sign the assembly

我在尝试使用 Microsoft's Strong Name Tool (SN.exe) 创建新的签名密钥时能够解决问题,但出现访问被拒绝的错误。

sn -k jeff.snk
Failed to generate a strong name key pair -- Access is denied.

使用 Process Monitor 告诉我问题是对 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 中的特定文件的访问被拒绝。在授予我对目录中所有文件的权限后,我能够创建签名密钥和 运行 msbuild 而没有任何错误。