Azure SQL DBの認証にAzure ADを利用している場合にプログラムからアクセスしたいとします。
Azure AD認証を行う方法は色々とあるのですが、Azure ADにアプリケーションを登録するところから始めると色々と面倒だったりもしますよね、管理者が登録を禁止してたりとか…。
そんな時に一番簡単にできるのはMicrosoft.Data.SqlClientを使ってインタラクティブにAAD認証する方法だと思います。これならアプリケーションのAADへの登録すら必要ありません。
using System;
using Microsoft.Data.SqlClient;
namespace AzureSQLDBAuthentication
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = @"Server=xxxsqlservernamexxx.database.windows.net; Authentication=Active Directory Interactive; Database=testdb;";
using (SqlConnection conn = new SqlConnection(ConnectionString)) {
conn.Open();
}
}
}
}
これだけでOKです。
使っているのは下記です。
Microsoft.Data.SqlClient 3.0.1
Provides the data provider for SQL Server. These classes provide access to versions of SQL Server and encapsulate database-specific protocols, including tabular...
コメント