Search code examples
azureazure-active-directoryazure-functionsasp.net-core-2.1azure-authentication

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.


Solution

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