본문 바로가기

카테고리 없음

ASP.NET 5.0 미만에서 PostgreSQL SSL로 접근하는 방법

  1. NuGet 패키지 관리에서 Npgsql 를 추가한다.
  2.  
public string NpgsqlGetConnectionString()
{


    var builder = new NpgsqlConnectionStringBuilder();


    builder.Host = "host";
    builder.Port = 5432;
    builder.Database = "db";
    builder.Username = "username";
    builder.Password = "password";
    builder.Pooling = false;

    builder.SslMode = SslMode.Require;
    builder.TrustServerCertificate = true;


    builder.SslCertificate = @"D:\sources\EntityGenerator\EntityGenerator\cert\postgresql.pfx";
    builder.SslPassword = "sslpassword";    
    builder.RootCertificate = @"D:\sources\EntityGenerator\EntityGenerator\cert\root.crt";

    return builder.ToString();
}

var connectionString = NpgsqlGetConnectionString();

using (NpgsqlConnection con = new NpgsqlConnection(connectionString))
{
    //Microsoft.SqlServer.Management.Smo.Server server;
    //Microsoft.SqlServer.Management.Common.ServerConnection serverConnection =
    //  new Microsoft.SqlServer.Management.Common.ServerConnection(con);

    NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(query, con);
    adapter.Fill(ds);
}

APS.NET 5.0 미만일 경우 sslcertificat 에 crt파일로 pem파일 형식으로 넣으면 안된다

pfx파일로 만들어서 넣어주자. pfx로 만들경우 sslpassword가 필요하다

삽질해서 한거니 안될 경우 다른 방법으로 하자