【问题标题】:EF Core + Azure PostgreSQL with Managed Identity Documentation?带有托管标识文档的 EF Core + Azure PostgreSQL?
【发布时间】:2021-01-18 16:54:26
【问题描述】:

MS 有 EF Core + Azure SQL with Managed Identity 的文档。两年前的This SO post 也对它以及一些替代实现进行了深入讨论。

但我找不到与 EF Core 一起使用的 Azure PostgreSQL 的任何内容,它也支持托管标识。

MS 在此处提供了 Azure PostgreSQL 托管标识的通用文档: https://docs.microsoft.com/en-us/azure/postgresql/howto-connect-with-managed-identity

这似乎表明在常规 PostgreSQL 连接字符串中用访问令牌替换密码是它的工作原理。

那么使用 EF Core 实现这一点的最佳方法是什么?

任何建议或相关文档的链接将不胜感激。

【问题讨论】:

    标签: entity-framework asp.net-core entity-framework-core azure-managed-identity azure-postgresql


    【解决方案1】:

    在常规的 PostgreSQL 连接字符串中用访问令牌替换密码是它的工作原理。

    在 .NET Core 中,通常会这样配置:

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

    【讨论】:

    • 我假设我们可以使用相同的Microsoft.Azure.Services.AppAuthentication 包作为令牌?同样在 2 年前查看该线程,您似乎建议 GetAccessTokenAsync().Result 阻塞调用不太可能由于缓存而导致性能问题,而有些人推荐 DbInterceptor。在构造函数或 DI 配置中简单地调用 GetAccessTokenAsync().Result 仍然是官方立场吗?谢谢。
    • AppAuthentication 进行令牌缓存,所以我认为这没什么大不了的,但使用拦截器也是一个很好的模式,如果稍微复杂一点的话。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2022-06-14
    • 2022-11-11
    • 2020-12-30
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多