我正在使用 NpgSqlConnection 在 .net core web api 项目中。当前的 PostgreSQL 服务器已被移动到另一个服务器,并需要使用客户端证书进行连接。我们提供了3个证书文件,命名为client-cert.pem,client-key.pem和server-ca.pem。我能够通过从浏览器的 pgAdmin 使用客户端证书和密钥文件连接到服务器,但是我无法从我的代码连接。尝试了互联网上的几种方法,但仍然遇到以下错误。
{"28000: 连接需要一个有效的客户端证书"}
我正在尝试的代码片段如下所示。
var connectionString = "User ID=" + _dOptions.Value.AuthenticationCredentials.UserName
+ ";Password=" + _dOptions.Value.AuthenticationCredentials.PassWord
+ ";Server=" + _dOptions.Value.Server
+ ";Port=" + _dOptions.Value.Port.ToString()
+ ";Database=" + _dOptions.Value.Database
+ ";Integrated Security=true;Pooling=true;SSL Mode=Require;Trust Server Certificate=true";
_con = new NpgsqlConnection(connectionString);
_con.ProvideClientCertificatesCallback += new ProvideClientCertificatesCallback(MyClientCertificates);
private void MyClientCertificates(X509CertificateCollection certificates)
{
var cert = new X509Certificate("C:\\Users\\c-Anish\\Documents\\cloud_sql_replica_certs\\DEV\\client-cert.pem");
certificates.Add(cert);
}
此外,我们在此仅使用名为client-cert.pem的客户端证书,但我认为我们可能需要使用client-key.pem?如果是这样,我该如何添加它?我漏掉了什么吗?
由于我被卡在这个问题上,所以任何帮助将不胜感激。
PGSSLCERT
、PGSSLKEY
和PGSSLROOTCERT
,指向相应的文件。 - clamp