1

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://learn.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.

thankyoussd
  • 1,875
  • 1
  • 18
  • 39

1 Answers1

2

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);
            });


    }
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • I assume we can use the same ```Microsoft.Azure.Services.AppAuthentication``` package for token? Also looking through that thread 2 years ago, you seemed to suggest that the ```GetAccessTokenAsync().Result``` blocking call is unlikely going to cause performance issues due to cache, while some recommended ```DbInterceptor```. Is it still the official stance to simply call ```GetAccessTokenAsync().Result``` in constructor or DI configuration? Thanks. – thankyoussd Jan 18 '21 at 18:25
  • 1
    The AppAuthentication does token caching so I don't think it's a big deal, but using a interceptor is a good pattern too, if a bit more complex. – David Browne - Microsoft Jan 18 '21 at 19:19