Batec.Azure.Data.Extensions.MySqlConnector 1.0.1

dotnet add package Batec.Azure.Data.Extensions.MySqlConnector --version 1.0.1                
NuGet\Install-Package Batec.Azure.Data.Extensions.MySqlConnector -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Batec.Azure.Data.Extensions.MySqlConnector" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Batec.Azure.Data.Extensions.MySqlConnector --version 1.0.1                
#r "nuget: Batec.Azure.Data.Extensions.MySqlConnector, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Batec.Azure.Data.Extensions.MySqlConnector as a Cake Addin
#addin nuget:?package=Batec.Azure.Data.Extensions.MySqlConnector&version=1.0.1

// Install Batec.Azure.Data.Extensions.MySqlConnector as a Cake Tool
#tool nuget:?package=Batec.Azure.Data.Extensions.MySqlConnector&version=1.0.1                

Authentication helper library for Azure Database for MySQL

Azure Database for MySQL accepts using an Azure AD issued access token as password. That access token should be issued for a specific audience https://ossrdbms-aad.database.windows.net/.default. As described in README there are some concepts to keep in mind, such as access token caching and connection pool fragementation. This library provides some utilities to facilitate the connection to MySQL without worring for those implementation details. For that purpose, this library provide extensions to MySqlConnector library.

MySqlConnector

MySqlConnector exposes a property in MySqlConnection to assign a delegate to obtain a password when a new connection is being created. See MySqlConnection.ProvidePasswordCallback property for details.

TokenCredentialMysqlPasswordProvider

TokenCredentialMysqlPasswordProvider provides the method ProvidePasswordAsyncProvidePasswordAsync that is compatible with MySqlConnection.ProvidePasswordCallback.

TokenCredentialMysqlPasswordProvider can be configured in different ways to retrieve the access token:

  • Using DefaultAzureCredential. This component has a fallback mechanism trying to get an access token using different mechanisms. This is the default implementation.
  • Specify an Azure Managed Identity. It uses DefaultAzureCredential, but tries to use a specific Managed Identity if the application hosting has more than one managed identity assigned.
  • Specify a TokenCredential. It uses a TokenCredential provided by the caller to retrieve an access token.

Sample using DefaultAzureCredential with TokenCredentialMysqlPasswordProvider

This sample uses DefaultAzureCredential to obtain an access token. If you execute this sample in your local development environment it can take the credentials from environment variables, your IDE (Visual Studio, Visual Studio Code, IntelliJ) or Azure cli, see DefaultAzureCredential for more details.

TokenCredentialMysqlPasswordProvider passwordProvider = new TokenCredentialMysqlPasswordProvider(new DefaultAzureCredential());
using MySqlConnection connection = new MySqlConnection(configuration.GetConnectionString());
connection.ProvidePasswordCallback = passwordProvider.ProvidePassword;
await connection.OpenAsync();

Sample using TokenCredentialMysqlPasswordProvider with a Managed Identity

It is necessary to pass the managed identity client id, not the object id.

string managedIdentityClientId = "00000000-0000-0000-0000-000000000000";
TokenCredentialMysqlPasswordProvider passwordProvider = new TokenCredentialMysqlPasswordProvider(new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = managedIdentityClientId }));
using MySqlConnection connection = new MySqlConnection(GetConnectionString());
connection.ProvidePasswordCallback = passwordProvider.ProvidePassword;
await connection.OpenAsync();

You can use the following command to retrieve the managed identity client id:

az identity show --resource-group ${RESOURCE_GROUP} --name ${MSI_NAME} --query clientId -o tsv

Sample using TokenCredentialMysqlPasswordProvider using an AzureCliCredential

This sample uses the TokenCredentialMysqlPasswordProvider constructor passing a TokenCredential. For simplicity this sample uses Azure cli credential

AzureCliCredential credential = new AzureCliCredential();
TokenCredentialMysqlPasswordProvider passwordProvider = new TokenCredentialMysqlPasswordProvider(credential);
using MySqlConnection connection = new MySqlConnection(GetConnectionString());
connection.ProvidePasswordCallback = passwordProvider.ProvidePassword;
await connection.OpenAsync();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Batec.Azure.Data.Extensions.MySqlConnector:

Package Downloads
Batec.Azure.Data.Extensions.Pomelo.EntityFrameworkCore

This is the Batec.Azure.Data.Extensions.Npgsql.EntityFrameworkCore client library for developing .NET applications that uses AzureAD authentication for EntityFrameworkCore to connect to MySql databases.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 215 8/6/2023
1.0.0 277 7/28/2023