Skip to main content

How to Use TLS 1.3 with MySql.Data in .NET C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

MySQL 9.7 allows TLS 1.3, and .NET 10 supports it, yet MySql.Data connects with TLS 1.2. It's the driver's default, on every platform. One connection string option changes that, and macOS needs one more setting.

See What You Have​

The one-file tester from How to Write a MySQL Connection String for .NET C# prints the TLS version in use:

Terminal
export MYSQL_CONNECTION_STRING="Server=127.0.0.1;Port=3306;Database=inventory;User ID=inventory_app;Password=$(grep '^MYSQL_PASSWORD=' ../dewiride-inventory-api/.env | cut -d= -f2-);SslMode=Required"
dotnet run test-connection.cs
Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.2

I got TLS 1.2 on macOS, and in a Linux container too. The reason is one line in the driver's source, at tag 26.10.0:

Ssl.cs
tlsProtocol = (tlsProtocol == SslProtocols.None) ? SslProtocols.Tls12 : tlsProtocol;

Step 1: Add TlsVersion​

List the versions the driver may use. This suits most apps:

Terminal
export MYSQL_CONNECTION_STRING="Server=127.0.0.1;Port=3306;Database=inventory;User ID=inventory_app;Password=$(grep '^MYSQL_PASSWORD=' ../dewiride-inventory-api/.env | cut -d= -f2-);SslMode=Required;TlsVersion=TLSv1.2,TLSv1.3"
dotnet run test-connection.cs
Where it runsTLS version it used
A Linux containerTLS 1.3
WindowsI didn't test this
macOSTLS 1.2
Output, on macOS
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.2

Listing both keeps it working everywhere. Naming only TLSv1.3 fails on macOS:

Output, with TlsVersion=TLSv1.3
Unhandled exception. System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> System.PlatformNotSupportedException: The requested security protocol is not supported.

Step 2: On macOS, Turn On Network.framework​

.NET on macOS uses Apple's Secure Transport by default. Microsoft's .NET 10 notes explain: "Historically, macOS used Secure Transport which doesn't support TLS 1.3; opting into Network.framework enables TLS 1.3." It's a switch for the process:

Terminal
export DOTNET_SYSTEM_NET_SECURITY_USENETWORKFRAMEWORK=1
dotnet run test-connection.cs
Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.3

The test program printing TLS: TLSv1.2 without the Network.framework switch and TLS: TLSv1.3 with it, with both versions listed in TlsVersion.

It takes both: with the switch and no TlsVersion, the driver still asks for 1.2.

For development on a Mac, you can leave this out. TLS 1.2 with a modern cipher is still secure, and your production server probably isn't a Mac.

Do You Need It?​

TLS 1.2 is fine for most people. The reason to set this is a server that accepts only 1.3, because without it you get a baffling Cannot determine the frame size or a corrupted frame was received:

Output, against a server with only TLS 1.3
Unhandled exception. System.Security.Authentication.AuthenticationException: Cannot determine the frame size or a corrupted frame was received.

And with TlsVersion=TLSv1.3 and the switch, against that same server:

Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.3

Listing both versions costs nothing, so it's worth having in a production connection string.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming the driver picks the best versionIt picks 1.2Set TlsVersion
Naming only TLSv1.3 in a shared stringIt fails on a MacList both
Setting the switch and no TlsVersionIt still asks for 1.2Set both

Troubleshooting​

SymptomLikely causeFix
The requested security protocol is not supportedOnly 1.3, on macOS, with no switchThe fix
Cannot determine the frame size…No version in common with the serverThe fix
TLS protocols TLSv1 and TLSv1.1 are no longer supportedAn old valueThe fix

Conclusion​

Add TlsVersion=TLSv1.2,TLSv1.3, and each platform uses the best it can. On macOS, TLS 1.3 also needs DOTNET_SYSTEM_NET_SECURITY_USENETWORKFRAMEWORK=1.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.