相关疑难解决方法(0)

Npgsql与.net core web api中的ssl证书连接

我正在使用 NpgSqlConnection for .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);
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)

另外,这里我们仅使用名为 client-cert.pem 的客户端证书,但是,我认为我们可能需要使用 client-key.pem ?如果是这样,我该如何添加?我在这里缺少什么?

任何帮助将不胜感激,因为我一直在解决这个问题。

c# postgresql ssl-certificate npgsql .net-core

5
推荐指数
1
解决办法
4651
查看次数

标签 统计

.net-core ×1

c# ×1

npgsql ×1

postgresql ×1

ssl-certificate ×1