유효한 SSL 인증서 만 수락하도록 구성된 PostgreSQL 데이터베이스에 연결하는 데 문제가 있습니다. 적절한 인증서와 키를 사용하여 pgAdmin III를 사용하여 연결할 수 있지만 Npgsql을 사용하여 작업 할 수 없습니다. 데이터베이스 연결을 열려고하면 System.IO.IOException: The authentication or decryption has failed
이 표시됩니다.Npgsql 및 SSL 인증서 인증을 사용할 때 SSL 오류가 발생했습니다.
NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder();
csb.Database = "database";
csb.Host = "psql-server";
csb.UserName = "dreamlax"; // must match Common Name of client certificate
csb.SSL = true;
csb.SslMode = SslMode.Require;
NpgsqlConnection conn = new NpgsqlConnection(csb.ConnectionString);
conn.ProvideClientCertificatesCallback += new ProvideClientCertificatesCallback(Database_ProvideClientCertificatesCallback);
conn.CertificateSelectionCallback += new CertificateSelectionCallback(Database_CertificateSelectionCallback);
conn.CertificateValidationCallback += new CertificateValidationCallback(Database_CertificateValidationCallback);
conn.PrivateKeySelectionCallback += new PrivateKeySelectionCallback(Database_PrivateKeySelectionCallback);
conn.Open(); //System.IO.IOException: The authentication or decryption has failed
콜백은 다음과 같이 정의된다 : I는 각 콜백이 유효 뭔가를 반환 만, 여전히 예외 : IOException가 슬로우되는 것을 확인 중단 점을 설정
static void Database_ProvideClientCertificates(X509CertificateCollection clienteCertis)
{
X509Certificate2 cert = new X509Certificate2("mycert.pfx", "passphrase");
clienteCertis.Add(cert);
}
static X509Certificate Database_CertificateSelectionCallback(X509CertificateCollection clientCerts, X509Certificate serverCert, string host, X509CertificateCollection serverRequestedCerts)
{
return clienteCertis[0];
}
static AsymmetricAlgorithm Database_PrivateKeySelectionCallback(X509Certificate cert, string host)
{
X509Cerficate2 thisCert = cert as X509Certificate2;
if (cert != null)
return cert.PrivateKey;
else
return null;
}
static bool MyCertificateValidationCallback(X509Certificate certificate, int[] certificateErrors)
{
// forego server validation for now
return true;
}
여기 내 코드입니다.
의견을 보내 주셔서 감사합니다. 패치로 풀 요청을 만드시겠습니까? http://git.npgsql.org 이것은 Npgsql과 SSL을 사용할 때 오류를받는 다른 사람들에게 매우 유용 할 것입니다. 미리 감사드립니다. –
@FranciscoJunior : 안녕하세요, 요청을 생성했습니다. 나는 git를 처음 사용하기 때문에 제대로했는지 확신 할 수 없습니다. – dreamlax