如何在 Visual Studio (SQL Server CLR) 中使用证书签署 DLL

问题描述 投票:0回答:1

我尝试在 Visual Studio 数据库项目 (SSDT) 中对 CLR 程序集进行签名,以便可以将程序集发布到 SQL Server,但没有成功。

我手动完成的操作并且有效

  1. 在 SQL Server 中创建证书并将其导出

  2. 然后我将证书格式PVK转换为PFX

pvk2pfx.exe -pvk C:\Certs\ClrCert.pvk -spc C:\Certs\ClrCert.cer -pfx C:\Certs\ClrCert.pfx -pi Pa$$w0rd -po Pa$$w0rd
  1. 使用 PFX 证书签署 DLL

DLL位于Visual Studio项目的BIN文件夹中。

signtool.exe sign /fd SHA256 /f C:\Certs\ClrCert.pfx /p Pa$$w0rd C:\...\bin\Release\ClrTest.dll 
  1. 导入签名的程序集并执行它
-- Import assembly
CREATE ASSEMBLY CLRTest FROM 'C:\...\CLRTest.dll' WITH PERMISSION_SET = SAFE;
GO

-- Import function from the assembly
CREATE FUNCTION [dbo].[FooFunction] (@year INT NULL)
RETURNS BIT
AS EXTERNAL NAME [CLRTest].[UserDefinedFunctions].[FooFunction]

-- Execute function
select dbo.FooFunction(2022)

尝试 1 - 在构建后事件中签署程序集

  1. 设置事件

Project > Properties > Build events : Post-build event command line

"C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\signtool.exe" sign /fd SHA256 /f C:\Certs\ClrCert.pfx /p Pa$$w0rd "C:\Workspace\CLRTest\bin\Release\ClrTest.dll"
  1. 点击构建

我可以在输出窗口中看到 DLL 已签名

Done Adding Additional Store Successfully signed:
C:\Workspace\CLRTest\bin\Release\CLRTest.dll
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
  1. 部署

当我执行发布配置文件时,我看到此错误

CREATE or ALTER ASSEMBLY for assembly 'CLRTest' with the SAFE or EXTERNAL_ACCESS option failed because the 'clr strict security' option of sp_configure is set to 1. 
Microsoft recommends that you sign the assembly with a certificate or asymmetric key...

这是为什么?

尝试 2 - 使用 Visual Studio 向导对程序集进行签名

  1. 打开窗户

Project > Properties > SQLCLR > Signing

  1. 选择证书并提供密码

  1. 点击构建

我收到此错误,无法导入密钥

Cannot import the following key file: ..\..\..\..\..\Certs\ClrCert.pfx. The key file may be password protected.

我做错了什么? 🙈

deployment certificate visual-studio-2022 sql-server-data-tools sqlclr
1个回答
0
投票

我遇到了一些问题,只是无法在 Visual Studio 中分配 c# CLR 项目,甚至遵循了所有谷歌搜索的解决方案。

原来有一个非常简单的方法:

只需在 Visual Studio 中创建 pfx 文件,在“选择强名称密钥文件”下拉列表中选择“”。

© www.soinside.com 2019 - 2024. All rights reserved.