I'm using the NpgSqlConnection for .net core web api project. Current PostgreSQL server has been move to another one and need to connect using the client certificates. We are provided with 3 certificate files named client-cert.pem, client-key.pem and server-ca.pem. I was able to connect to the server using the client cert & key files through the pgAdmin from browser. But, I'm not able to connect from my code. Tried several methods from internet but still I'm getting the below error.
{"28000: connection requires a valid client certificate"}
The code snippet I'm trying is given below.
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);
}
Also, here we are using only the client certificate named client-cert.pem, but, I think we may need to use the client-key.pem ? If so, how can I add that ? What am I missing here ?
Any help will be highly appreciated as I'm stuck with this issue.
PGSSLCERT,PGSSLKEYandPGSSLROOTCERTthat point to the files.