This is the Batec.Azure.Data.Extensions.Npgsql.EntityFrameworkCore client library for developing .NET applications that uses AzureAD authentication for EntityFrameworkCore to connect to Postgresql databases.
License
—
Deps
4
Install Size
—
Vulns
✓ 0
Published
Aug 6, 2023
$ dotnet add package Batec.Azure.Data.Extensions.Npgsql.EntityFrameworkCoreThis library provides some extension methods to facilitate the usage of Azure AD authentication when connecting to Azure Database for Postgresql.
DbContextOptionsBuilder is used to configure the Entity Framework context. This library provides the UseAzureADAuthentication method to configure PostgreSQL connections.
This library uses Batec.Azure.Data.Extensions.Npgsql library to get an Azure AD access token that can be used to authenticate to Postgresql. This method expects a TokenCredential, here some examples that can be used:
Using DefaultAzureCredential to get a token.
// configuring services
var services = new ServiceCollection();
services.AddDbContextFactory<SampleContext>(options =>
{
options.UseNpgsql("POSTGRESQL CONNECTION STRING",
npgsqlOptions => npgsqlOptions.UseAzureADAuthentication(new DefaultAzureCredential())); // Usage of this library
});
It uses UseAzureADAuthentication passing the client id of the prefered managed identity in case the hosting service has more than one assigned. It uses DefaultAzureCredential passing the preferred managed identity.
// configuring services
var services = new ServiceCollection();
string managedIdentityClientId = "00000000-0000-0000-000000000000";
services.AddDbContextFactory<SampleContext>(options =>
{
options.UseNpgsql("POSTGRESQL CONNECTION STRING",
npgsqlOptions => npgsqlOptions.UseAzureADAuthentication(new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = managedIdentityClientId })));
});
It uses UseAzureADAuthentication passing a TokenCredential provided by the caller. For simplicity, this sample use AzureCliCredential
// configuring services
AzureCliCredential tokenCredential = new AzureCliCredential();
var services = new ServiceCollection();
services.AddDbContextFactory<ChecklistContext>(options =>
{
options.UseNpgsql("POSTGRESQL CONNECTION STRING",
options => options.UseAzureADAuthentication(tokenCredential)); // Usage of this library
});