7

I am trying to connect to Azure db with Azure AD credentials through c# code (Code is below). It works fine on my system. But when I deploy it to a 32 bit VM, it shows error

"Keyword not supported : authentication".

The VM has .Net framework 4.5 installed (But not Visual Studio). Application is targeting .Net Framework 4.5.

As per my observations, system.data for framework 2.0 does not support authentication keyword for SQLConnection class. But my application is targetting 4.5 , so it should work fine with 4.5 installed. can anyone help to resolve it. Below is my code

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string ConnectionString =
      @"Data Source=mydatabase.database.windows.net; Authentication=Active Directory Password; UID=user.name@microsoft.contoso.com; PWD=Test@pswd";
                SqlConnection conn = new SqlConnection(ConnectionString);
                conn.Open();
                Console.WriteLine("connected");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
Marat
  • 15,215
  • 2
  • 39
  • 48
Vineet Kumar
  • 176
  • 2
  • 11
  • Possible duplicate of [Azure Active Directory Connection String](http://stackoverflow.com/questions/34943369/azure-active-directory-connection-string) – Hello Feb 14 '17 at 08:45
  • 1
    This links suggests .Net Framework 4.6. Installed it and the error is gone, however it needs adalsql.dll. It looks for the dll in system32 directory. After installing Active Directory for Sql Server msi from microsoft it worked. But my problem now is, i want to make it work without having my customers to install azure active directory msi. – Vineet Kumar Feb 15 '17 at 10:14
  • I tried adding reference and making it copy to local in a hope that it will remove the dependency, but it did not help. I want to embed this dll into my installer so that end user would not have to install msi explicitly – Vineet Kumar Feb 15 '17 at 10:16
  • 3
    @VineetKumar - Did you resolve this issue as I am now having same issue – Chris Hammond May 22 '18 at 10:46
  • I am facing this exact same issue – Praveen Rao Chavan.G May 15 '22 at 14:47

1 Answers1

3

This answer is regarding dotnet core and not .NET Framework 4.5/6, so it does not directly answer your question. However, it may help you with respect to deployment of code to your customers.

I encountered the same error attempting to connect from dotnet-core. From Microsoft:

Starting with .NET Core 2.2, an access token issued by Azure Active Directory can be used to authenticate to an Azure SQL database. To support access tokens, the AccessToken property has been added to the SqlConnection class. To take advantage of AAD authentication, download version 4.6 of the System.Data.SqlClient NuGet package. In order to use the feature, you can obtain the access token value using the Active Directory Authentication Library for .NET contained in the Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package.

Eric Patrick
  • 2,097
  • 2
  • 20
  • 31
  • These instructions (given by Microsoft) are very unclear, I've tried doing this using these instructions >> https://learn.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi#modify-aspnet-core, yet when I specify a user ID I get `Tried to get token using Managed Service Identity. Access token could not be acquired. Connection refused` and when I don't specify a user I (predictably) get `Login failed for user ''`. – ataraxia Aug 23 '19 at 23:26