【问题标题】:can not connect Azure SQL with Azure AD user in Azure无法将 Azure SQL 与 Azure 中的 Azure AD 用户连接
【发布时间】:2020-05-31 02:16:18
【问题描述】:
我有一个用于使用 Azure AD 用户查询 Azure SQL 数据库的 powershell 脚本。我可以在本地机器上运行 poweshell 脚本。但是当我在 Azure 函数上托管 powershell 脚本时,我总是收到错误消息:keyword not supported: 'authentication'
我的脚本
$Username = “”
$Password = “”
$Port = 1433
$cxnString = "Server=tcp:$serverName,$Port;Database=$databaseName;UID=$UserName;PWD=$Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;Authentication=Active Directory Password"
$query = ''
Invoke-Sqlcmd -ConnectionString $cxnString - Query $query
【问题讨论】:
标签:
powershell
azure-functions
azure-sql-database
【解决方案1】:
根据我的测试,现在我们无法在 Azure Function 中使用 PowerShell 来实现它。现在我们可以使用 .Net Core Function 来实现它。我们需要打包Microsoft.Data.SqlClient
例如
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
string userName = "<>";
string password = "<>";
string server = "<>.database.windows.net";
string database = "test";
builder.ConnectionString = "Server=tcp:" + server + ",1433;Initial Catalog=" + database + ";Authentication=Active Directory Password;User ID=" + userName + ";Password=" + password + ";MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
string sql = "SELECT * FROM StarWars";
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
log.LogInformation(reader.GetString(2));
}
}
}
}