【发布时间】:2020-10-08 11:14:05
【问题描述】:
我正在使用 NpgSqlConnection for .net core web api 项目。当前 PostgreSQL 服务器已移至另一台服务器,需要使用客户端证书进行连接。我们提供了 3 个证书文件,分别名为 client-cert.pem、client-key.pem 和 server-ca.pem。我能够通过浏览器中的 pgAdmin 使用客户端证书和密钥文件连接到服务器。但是,我无法从我的代码连接。从互联网上尝试了几种方法,但我仍然收到以下错误。
{"28000: 连接需要有效的客户端证书"}
我正在尝试的代码 sn-p 如下所示。
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。 -
我试过了,但得到了同样的错误信息。
-
还没有找到解决办法。谁能帮忙?
标签: c# postgresql .net-core ssl-certificate npgsql