3
votes

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

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

1
can you add you code snippet for better clarity... - Md Farid Uddin Kiron
This is my local.settings connection string "ConnectionString": "Data Source=tcp:********.windows.net,1433;Initial Catalog=********;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=Active Directory Integrated", and when creating context with EF core i'm getting the error in below line of code WriteLogMessage($"Database Connection set to {Database.GetDbConnection().Database} and data source {Database.GetDbConnection().Database}") - suresh
@suresh could you post the error message and full call stack. it's diffcult to help without sufficient information - axfd

1 Answers

6
votes

UPDATE - 16/08/2019
Active Directory Password Authentication has now been added for .NET Core in Microsoft.Data.SqlClient 1.0.19221.1-Preview


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

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.

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.

Here is the same for reference

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

The connection string would be something like this
Server=tcp:<server_name>.database.windows.net,1433;Database=<db_name>;