Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure SQL: Microsoft.Data.SqlClient - Invalid value for key 'authentication' #10357

Open
techsaau opened this issue Sep 23, 2024 · 4 comments
Open

Comments

@techsaau
Copy link

techsaau commented Sep 23, 2024

Description

I'm trying to connect to Azure SQL Database with "Active Directory Service Principal", It failed with below error:

But Same code works fine with "Active Directory Password" mode.

It looks like, by default it only uses System.Data.SqlClient and we are not able to use Microsoft.Data.SqlClient

Steps to reproduce

Try to do auth with AAD using SPN:

<PrivilegedContext Provider="Microsoft.Data.SqlClient" ConnectionString="Server=.database.windows.net;Authentication=Active Directory Service Principal; Encrypt=True; Database=-dbname; User Id={appID}; Password={secret}" CommandTimeout="3000" />

Expected behavior

Authentication should work and it is working in local Visual Studio.

Actual behavior

Authentication should be successful with SPN AppID & Secret

Diagnostic logs

Error Message:
   Assembly Initialization method XXXX.UnitTest.SqlDatabaseSetup.InitializeAssembly threw exception. System.ArgumentException: System.ArgumentException: Invalid value for key 'authentication'.. Aborting test execution.
  Stack Trace:
      at System.Data.Common.DbConnectionStringBuilderUtil.ConvertToAuthenticationType(String keyword, Object value)
   at System.Data.SqlClient.SqlConnectionStringBuilder.ConvertToAuthenticationType(String keyword, Object value)
   at System.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnectionStringBuilder..ctor(String connectionString)
   at Microsoft.Data.Tools.Schema.Sql.UnitTesting.Common.SqlConnectionStringSecurer.RestoreConnectionString(String partialConnection)
   at Microsoft.Data.Tools.Schema.Sql.UnitTesting.SqlDatabaseTestService.DecryptConnectionString(String connectionString)
   at Microsoft.Data.Tools.Schema.Sql.UnitTesting.SqlDatabaseTestService.SetupConnectionString(String connStr)
   at Microsoft.Data.Tools.Schema.Sql.UnitTesting.SqlDatabaseTestService.DeployDatabaseProject()
   at XXXX.UnitTest.SqlDatabaseSetup.InitializeAssembly(TestContext ctx) in C:\Data\Agent\_work\316\s\XXXX.UnitTest\SqlDatabaseSetup.cs:line 23

Environment

Azure DevOps Pipeline VSTest@3
vsTestVersion=latest

@nohwnd
Copy link
Member

nohwnd commented Sep 23, 2024

Do you have an example of how you are connecting to the database? In the stack trace I see you are running AssemblyInitialize method, in which you can do anything. Are you using some feature provided by VSTest to do the authentication?

@techsaau
Copy link
Author

techsaau commented Sep 23, 2024

I'm doing default code for Unit Testing and in which it has default code for the same:

[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
    // Setup the test database based on setting in the
    // configuration file
    SqlDatabaseTestClass.TestService.DeployDatabaseProject();
    SqlDatabaseTestClass.TestService.GenerateData();
}

And below is the app.config code block which I use to connect to DB:

<PrivilegedContext Provider="Microsoft.Data.SqlClient" ConnectionString="Server=.database.windows.net;Authentication=Active Directory Service Principal; Encrypt=True; Database=-dbname; User Id={appID}; Password={secret}" CommandTimeout="3000" />

Same code is working in my local Visual Studio. I can authenticate against AAD using SPN.

@nohwnd
Copy link
Member

nohwnd commented Sep 23, 2024

I am trying to find who owns that library.

@MichelZ
Copy link

MichelZ commented Nov 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants