By Kim Wee Sim
Junior Solution Architect
Hi guys,
I was working through one of my development projects and hit an obstacle with regards to using connection strings for .NET core to database managed instance. Is there any recommended solutions for direct connection to digital ocean DB cluster instance without PEM cert and key or alternatively, adding the cert and key into the application instead?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi!
I use a class like the following for connection to the database. If you want you can add the certificate to your application. Otherwise you must set the Field TrustServerSideCertificate to true
public class DatabaseSettings
{
public string Hostname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public bool DisableSSL { get; set; }
public int Port { get; set; } = 5432;
public bool TrustServerCertificate { get; set; } = false;
public string GetConnectionString()
{
var applicationName = Assembly.GetEntryAssembly().FullName;
var connectionBuilder = new NpgsqlConnectionStringBuilder
{
Host = Hostname,
Database = Name,
Username = Username,
Password = Password,
Port = Port,
MaxPoolSize = 50,
ApplicationName = applicationName,
TrustServerCertificate = TrustServerCertificate,
SslMode = DisableSSL ? SslMode.Disable : SslMode.Require,
};
return connectionBuilder.ConnectionString;
}
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.