不支持关键字:'authentication' Azure 集成连接错误

Keyword not supported: 'authentication' error for azure integrated connection

获取关键字不受支持:尝试通过 .NET Core 2.1 项目中的 'Active Directory Integrated' 选项连接 Azure 数据库时出现 'authentication' 错误。

注意:我正在使用 EF 核心连接数据源。

TL;DR 正如@Aamir Mulla 在评论中指出的那样,自 Version 2.0.0

以来已正式添加

更新 - 16/08/2019
现在已在 Microsoft.Data.SqlClient 1.0.19221.1-Preview

中为 .NET Core 添加 Active Directory 密码身份验证

遗憾的是,.NET Core 尚未完全支持 authentication 关键字。这是讨论这个的issue

但是 .NET Core 2.2 添加了对此 comment. The basic idea is to get the access token by any means(ADAL、REST 等)中提到的这种用例的一些支持,并为其设置了 SqlConnection.AccessToken

至于将其与 EF Core 一起使用,在线程中的 github issue and in particular the comment by mgolois provides a simple implementation to the solution that cbriaball mentions 中对此进行了很好的讨论。

这里也一样,供参考

Note that this sample is using the Microsoft.Azure.Services.AppAuthentication library

// DB Context Class
public class SampleDbContext : DbContext
{
  public SampleDbContext(DbContextOptions<TeamsDbContext> options) : base(options)
  {
    var conn = (System.Data.SqlClient.SqlConnection)this.Database.GetDbConnection();
    conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
  }
}

// Startup.cs
services.AddDbContext<SampleDbContext>(options =>
{
  options.UseSqlServer(<Connection String>);
});

连接字符串应该是这样的
Server=tcp:<server_name>.database.windows.net,1433;Database=<db_name>;