不支持关键字:azure 集成连接的“身份验证"错误

不支持关键字:azure 集成连接的“身份验证

问题描述:

不支持获取关键字:尝试通过 .NET core 2.1 项目中的Active Directory Integrated"选项连接 Azure DB 时出现身份验证"错误.

Getting Keyword not supported: 'authentication' error while trying to connect an azure DB through 'Active Directory Integrated' option in .NET core 2.1 project.

注意:我使用 EF core 连接数据源.

Note: I am using EF core to connect the Data source.

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

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

Unfortunately, the authentication keyword is not yet fully supported in .NET Core. Here is an issue which discusses this.

但是 .NET Core 2.2 已经添加了对此用例的一些支持,如本评论.基本思想是通过 任何方式(ADAL、REST 等)并将 SqlConnection.AccessToken 设置为它.

But .NET Core 2.2 has added some support for this use case as mentioned in this comment. The basic idea is to get the access token by any means (ADAL, REST, etc.) and set SqlConnection.AccessToken to it.

至于将其与 EF Core 一起使用,在此 github 问题中对此进行了很好的讨论 特别是 评论来自 mgoloiscbriaball 在线程中提到的解决方案.

As for using this with EF Core, there's a good discussion about this in this github issue and in particular the comment by mgolois provides a simple implementation to the solution that cbriaball mentions in the thread.

这里相同以供参考

请注意,此示例使用的是 Microsoft.Azure.Services.AppAuthentication 图书馆

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:.database.windows.net,1433;Database=;