Skip to content

Microsoft.Data.SqlClient not backward compatible with System.Data.Client, Error: Keyword not supported: 'trust server certificate'. #2762

Closed
@ashutoshsuman99

Description

@ashutoshsuman99

Describe the bug

A clear and concise description of what the bug is.

We have recently moved to Microsoft.Data.SqlClient from System.Data.SqlClient to build our connection strings and use it to connect to our backend database as well as to the customer database. But we are getting the following error.

Exception message: The connection string provided is invalid. Keyword not supported: 'trust server certificate'

Looks like the library Microsoft.Data.SqlClient adds spaces to TrustServerCertificate and makes the final string as:

image

Where as System.Data.SqlClient creates the connection string in the following format:

image

We have to use Microsoft.Data.SqlClient to support few other types of authentications as well like and work with services which that use this library, and we also work with services that use System.Data.SqlClient. And this interservice communication with connection string having spaces in TrustServerCertificate causes error when used with System.Data.SqlClient.

image

Is there any way for us to have a connection string without spaces in TrustServerCertificate using Microsoft.Data.SqlClient library?

Code to reproduce the error

    static void SqlConnectionCheck_TrustServerCertificate()
    {
        var mictosoftConnectionbuilder = new Microsoft.Data.SqlClient.SqlConnectionStringBuilder
        {
            DataSource = "localhost",
            InitialCatalog = "master",
            TrustServerCertificate = true,
            IntegratedSecurity = false,
            UserID = "testUser",
            Password = Guid.NewGuid().ToString()
        };

        var connectionString = mictosoftConnectionbuilder.ConnectionString;

        // Error occurs in this line - which is implemented in other service (not is our code)
        var systemConnectionBuidler = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);

        using (var connection = new System.Data.SqlClient.SqlConnection(systemConnectionBuidler.ConnectionString))
        {
            connection.Open();
            Console.WriteLine("Connected to the database successfully.");
        }

    }

Further technical details

Microsoft.Data.SqlClient version: (found on the nuget or Microsoft.Data.SqlClient.dll): 5.2.1
.NET target: (e.g. Framework 4.7.1, Core 2.2.2): .NET Framework 4.7.2
Operating system: (e.g. Windows 2019, Ubuntu 18.04, macOS 10.13, Docker container): Windows

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions