Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 224
suresh Avatar asked Jan 01 '23 05:01

suresh


2 Answers

TL;DR As called out by @Aamir Mulla in the comments, this has officially been added since Version 2.0.0


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

like image 71
PramodValavala-MSFT Avatar answered May 13 '23 11:05

PramodValavala-MSFT


If you're still having the issue, make sure you have Microsoft.Data.SqlClient package installed, not System.Data.SqlClient. They both contain SqlConnection class, switching the package for the first one fixed the issue for me.

like image 26
Peace Avatar answered May 13 '23 11:05

Peace