How to open a System.Data.SQLClient.SQLConnection with Active Directory Universal Authentication
Asked Answered
L

2

17

I was using the below code to connect to SQL Azure DB that was using Active Directory Integrated Authentication.

private string GenerateConnectionString(string databaseName, string serverName)
{
    SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
    sqlConnectionBuilder.DataSource = string.Format(
        "tcp:{0}.database.windows.net",
        serverName);
    connBuilder.InitialCatalog = databaseName;
    connBuilder.Authentication = SqlAuthenticationMethod.ActiveDirectoryIntegrated;
    connBuilder.ConnectTimeout = 30;
    return connBuilder.ConnectionString;
}

The authentication is changed from Active Directory Integrated Authentication to Active Directory Universal Authentication to support multi-factor authentication.

I see the enumeration System.Data.SqlClient.SqlAuthenticationMethod doesn't have a value for Active Directory Universal Authentication. Is it possible to still use the System.Data.SqlClient to connect to the DB? If yes, what is the change I have to do in the code?

enter image description here

Lawerencelawes answered 6/2, 2017 at 20:39 Comment(0)
D
3

ActiveDirectoryInteractive authentication method is available since the .NET Framework 4.7.2. Not sure if it is the same as "Universal" or not.

Detach answered 20/4, 2018 at 20:31 Comment(1)
.NET Framework 4.7.2 was not released when this question was first asked. Also, Active Directory Universal Authentication is indeed the same as ActiveDirectoryInteractive. See this: learn.microsoft.com/en-us/dotnet/framework/whats-new/…Morales
K
0

As of August 2020, there's a better way to connect to Azure SQL DB's or Azure Synapse DW (SQL pools). By using the MSOLEDBSQL driver (which you may redistribute along with your application), your application can perform interactive/MFA authentication using the normal System.Data.OleDb objects:

using System.Data.OleDb;
...
OleDbConnection con = new OleDbConnection("Provider=MSOLEDBSQL;Data Source=sqlserver.database.windows.net;User [email protected];Initial Catalog=database;Authentication=ActiveDirectoryInteractive");

In fact, this is the recommended way of connecting to any Microsoft SQL product programatically, as both SQLOLEDB and SQLNCLI (aka. "SNAC") have been deprecated.

Kenyettakenyon answered 31/8, 2020 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.