Visual Studio for Mac 对C#应用.app签名失败导致Apple公证异常
Visual Studio for Mac 下C#应用签名与Apple公证异常排查
问题背景
在Visual Studio for Mac环境下开发C#应用,已配置Developer ID签名以完成Apple公证流程,但当前流程运行异常。
操作过程与异常现象
- 通过Visual Studio for Mac执行代码签名
- 签名生成的.app文件启动即崩溃
- 打包为dmg格式后,通过
xcrun altool命令提交Apple公证,返回校验错误:{ "logFormatVersion": 1, "jobId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "status": "无效", "statusSummary": "压缩包存在关键校验错误", "statusCode": 4000, "archiveFilename": "MyApp.dmg", "uploadDate": "2020-08-18T08:29:55Z", "sha256": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "ticketContents": null, "issues": [ { "severity": "错误", "code": null, "path": "MyApp.dmg/MyApp.app/Contents/MonoBundle/SQLite.Interop.dll", "message": "该二进制文件的签名无效。", "docUrl": null, "architecture": "x86_64" } ] }
故障初步定位:即使公证流程通过,应用依然会崩溃,判断异常由SQLite.Interop.dll引发。在VS自动签名完成后,手动对该文件执行签名操作,执行命令如下:
$ codesign --force --verify --verbose \ --sign "Developer ID Application: MyCompany (XXXXXXXXX)" \ "SQLite.Interop.dll" \ --deep \ --options runtime \ --timestamp
手动签名后应用可正常启动,但再次提交公证时返回新的校验错误:
{ "logFormatVersion": 1, "jobId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "status": "无效", "statusSummary": "压缩包存在关键校验错误", "statusCode": 4000, "archiveFilename": "MyApp.dmg", "uploadDate": "2020-08-18T08:29:55Z", "sha256": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "ticketContents": null, "issues": [ { "severity": "错误", "code": null, "path": "MyApp.dmg/MyApp.app/Contents/MacOS/MyApp", "message": "该二进制文件的签名无效。", "docUrl": null, "architecture": "x86_64" } ] }
故障原因推测:修改SQLite.Interop.dll的签名后,主应用MyApp的原有签名失效,导致整体校验不通过。
已尝试的操作
- 手动重新签名
MyApp.dmg/MyApp.app/Contents/MacOS/MyApp主程序文件 - 在VS执行自动签名流程前,提前手动预签名
SQLite.Interop.dll
辅助排查参考信息
.csproj文件签名相关配置
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <Optimize>true</Optimize> <OutputPath>bin\Release</OutputPath> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <EnableCodeSigning>true</EnableCodeSigning> <CreatePackage>false</CreatePackage> <EnablePackageSigning>false</EnablePackageSigning> <IncludeMonoRuntime>true</IncludeMonoRuntime> <UseSGen>true</UseSGen> <UseRefCounting>true</UseRefCounting> <LinkMode>SdkOnly</LinkMode> <AOTMode>None</AOTMode> <CodeSigningKey>Developer ID Application: MyCompany (XXXXXXXXXXX)</CodeSigningKey> <PackageSigningKey>3rd Party Mac Developer Installer</PackageSigningKey> <UseHardenedRuntime>true</UseHardenedRuntime> <CodeSignEntitlements>Entitlements.plist</CodeSignEntitlements> <CodeSignExtraArgs>--deep</CodeSignExtraArgs> </PropertyGroup>
Entitlements.plist配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.security.cs.allow-jit</key> <true/> </dict> </plist>
SQLite依赖来源
当前使用的SQLite.Interop.dll来自NuGet的System.Data.SQLite.Core包,文件路径为:
packages/System.Data.SQLite.Core.1.0.111.0/runtimes/osx-x64/native/netstandard2.0/SQLite.Interop.dll
Visual Studio for Mac 构建日志(2020年8月31日15:52 补充)
Target GenerateSatelliteAssemblies: Creating directory "obj/Release/en". /Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/4.5/al.exe /culture:en /out:obj/Release/en/MyApp.resources.dll /template:obj/Release/MyApp.exe /embed:obj/Release/improvementproductivityforMac.Resources.AppResources.en.resources Target _CopyFilesMarkedCopyLocal: Copying file from "/Users/tomityu/Gits/my-app/packages/NLog.4.6.6/lib/netstandard2.0/NLog.dll" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/NLog.dll". Copying file from "/Users/tomityu/Gits/my-app/packages/System.Data.SQLite.Core.1.0.111.0/lib/netstandard2.0/System.Data.SQLite.xml" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/System.Data.SQLite.xml". Copying file from "/Users/tomityu/Gits/my-app/packages/NLog.4.6.6/lib/netstandard2.0/NLog.xml" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/NLog.xml". Copying file from "/Users/tomityu/Gits/my-app/packages/System.Data.SQLite.Core.1.0.111.0/lib/netstandard2.0/System.Data.SQLite.dll" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/System.Data.SQLite.dll". Copying file from "/Users/tomityu/Gits/my-app/packages/System.Data.SQLite.Core.1.0.111.0/lib/netstandard2.0/System.Data.SQLite.dll.config" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/System.Data.SQLite.dll.config". Creating "/Users/tomityu/Gits/my-app/MyApp/obj/Release/MyApp.csproj.CopyComplete" because "AlwaysCreate" was specified. Target CopyFilesToOutputDirectory: Copying file from "/Users/tomityu/Gits/my-app/MyApp/obj/Release/MyApp.exe" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.exe". MyApp -> /Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.exe Copying file from "/Users/tomityu/Gits/my-app/MyApp/obj/Release/en/MyApp.resources.dll" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/en/MyApp.resources.dll". Target _DetectSigningIdentity: Detected signing identity: Code Signing Key: "Developer ID Application: MYCOMPANY (7LM65QQ5VR)" (C34D25E1BBB78996C6A0257E78A87C103A320815) Bundle Id: jp.mycompany App Id: jp.mycompany Target _CopyContentToBundle: Creating directory '/Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.app/Contents/Resources' ~ Copying file from '/Users/tomityu/Gits/my-app/MyApp/Resources/update.png' to '/Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.app/Contents/Resources/update.png' Target _CompileToNative: /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/bin/mmp @/Users/tomityu/Gits/my-app/MyApp/obj/Release/response-file.rsp MMP : warning MM0079: The recommended Xcode version for Xamarin.Mac 6.20.2 is Xcode 11.6 or later. The current Xcode version (found in /Applications/Xcode.app/Contents/Developer) is 11.2. MMP : warning MM2006: Native library 'libdl' was referenced but could not be found. MMP : warning MM2009: Referenced by System.Data.SQLite.UnsafeNativeMethodsPosix.dlopen Done building target "_CompileToNative" in project "MyApp.csproj". Target CopyOurFiles: This is us copying a file into resources! Copying file from "/Users/tomityu/Gits/my-app/MyApp/NLog.config" to "/Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.app/Contents/MonoBundle/NLog.config". Target _CodesignNativeLibraries: Tool /usr/bin/codesign execution started with arguments: -v --force --timestamp --sign C34D25E1BBB78996C6A0257E78A87C103A320815 --deep /Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.app/Contents/MonoBundle/libmono-native.dylib Tool /usr/bin/codesign execution started with arguments: -v --force --timestamp --sign C34D25E1BBB78996C6A0257E78A87C103A320815 --deep /Users/tomityu/Gits/my-app/MyApp/bin/Release/MyApp.app/Contents/MonoBundle/libMonoPosixHelper.dylib Target _CodesignAppBundle: Tool /usr/bin/codesign execution started with arguments: -v --force -o runtime --timestamp --sign C34D25E1BBB78996C6A0257E78A87C103A320815 --entitlements /Users/tomityu/Gits/my-app/MyApp/obj/Release/Entitlements.xcent --deep /Users/tomityu/Gits/my-app/MyApp.app Target _CodesignVerify: /usr/bin/codesign --verify -vvvv --deep bin/Release/MyApp.app bin/Release/MyApp.app: valid on disk bin/Release/MyApp.app: satisfies its Designated Requirement Done building project "MyApp.csproj". Build succeeded.
根因定位
根据构建日志可确认,VS for Mac自动签名流程中,_CodesignNativeLibraries阶段仅扫描到Mono自带的两个dylib文件,未识别到被打包进MonoBundle目录的SQLite.Interop.dll,导致该文件未被正确签名。后续手动修改该文件签名时,打破了.app包的整体签名完整性,造成主程序签名校验失效。
解决操作步骤
- 配置原生依赖复制规则
- 在项目中添加对
SQLite.Interop.dll的直接引用,将其复制行为设置为“如果较新则复制”,确保该文件在构建阶段被提前放入MyApp.app/Contents/MonoBundle目录,而非依赖mmp自动打包。
- 在项目中添加对
- 调整签名参数配置
- 移除.csproj中
CodeSignExtraArgs属性的--deep参数,避免递归签名导致的覆盖问题。
- 移除.csproj中
- 按顺序执行签名流程
- 先对.app包内所有原生二进制文件(包括dylib、
SQLite.Interop.dll等)单独执行带--options runtime、--timestamp参数的Developer ID签名。 - 再对整个.app包执行最终签名,附加Entitlements.plist配置的权限项。
- 先对.app包内所有原生二进制文件(包括dylib、
- 前置校验
- 打包dmg前,执行
codesign --verify --deep --strict --verbose=2 /path/to/MyApp.app命令,确认所有文件签名有效。 - 打包dmg完成后,对dmg文件执行签名,再提交公证请求。
- 打包dmg前,执行
内容的提问来源于stack exchange,提问作者tomityu
相关产品推荐
相关产品推荐

