C#C
C#2y ago
TeBeCo

Azure - Postgre - EFCore Managed Identity

I'm looking for anyone that used EFCore in a WebApi and using Entra/AAD/ManagedIdentity to connect

currently i had ultra crap code that look like this:
        services.AddDbContext<MyDbContext>((sp, dbContextOptionsBuilder) =>
        {
            var sqlServerTokenProvider = new DefaultAzureCredential();
            var accessToken = sqlServerTokenProvider.GetTokenAsync(
                new TokenRequestContext(scopes: new string[] { "https://ossrdbms-aad.database.windows.net/.default" }))
            .GetAwaiter().GetResult(); // Fuck that shit

            var connectionString = $"Server=myserver.postgres.database.azure.com;Database=mydb;Port=5432;User Id=EMAIL YOU HAVE TO SET MANUALLY WHAT THE FUCK;Ssl Mode=prefer; Password={accessToken.Token}";

            dbContextOptionsBuilder.UseNpgsql(connectionString);
        });

this does not sounds normal at all that I have to set my own azure account email manually while I'm using DefaultAzureCredential
ManagedIdentity is supposed to be taken care from the DefaultAzureCredential itself
sounds like a big WTF happening here

can anyone show me how they connect to PostGre with a managed Identity ?
in particuliar:
  • connection string
  • shoudl work from AppService + local vs debugger
Was this page helpful?