© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
4 replies
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);
        });
        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
DefaultAzureCredential

ManagedIdentity is supposed to be taken care from the
DefaultAzureCredential
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Azure Managed Identity
C#CC# / help
15mo ago
Azure EventGrid Partner Topic Managed Identity access
C#CC# / help
8mo ago
❔ Azure DevOps API Authentication issue (Managed Identity)
C#CC# / help
3y ago
Using Azure Managed Identities API
C#CC# / help
3y ago