0
votes

MS has documentation for EF Core + Azure SQL with Managed Identity. This SO post from two years ago also had an in-depth discussion regarding it along with some alternative implementations.

But I cannot find anything for Azure PostgreSQL, which also supports managed identity, for use with EF Core.

MS has a generic documentation for Azure PostgreSQL managed identity here: https://docs.microsoft.com/en-us/azure/postgresql/howto-connect-with-managed-identity

It seems to suggest that replacing the password with access token in a regular PostgreSQL connection string is how it works.

So what is the best way to implement this with EF Core?

Any advice or link to related documentations would be greatly appreciated.

1

1 Answers

1
votes

replacing the password with access token in a regular PostgreSQL connection string is how it works.

In .NET Core that would typically be configured something like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services.AddTransient(typeof(Foo));
        services.AddSingleton(typeof(Bar));

        services.AddDbContext<Db>((sp, options) =>
            {
                var config = sp.GetRequiredService<IConfiguration>();
                var constr = config.GetConnectionString("ConnectionString");
                var tp = sp.GetService<ITokenProvider>();
                var token = tp.GetToken("https://ossrdbms-aad.database.windows.net");
                constr = constr.Replace("[passsword]", token);

                options.UseNpgsql(constr);
            });


    }