无法在启用 MSI 的函数应用程序中使用 PowerShell 连接到 Azure SQL

Cannot connect to Azure SQL using PowerShell in an MSI enabled function app

最近创建了一个函数应用 运行。函数应用程序托管一个 C# 和 PowerShell 函数,该函数在启用 MSI 的情况下按预期工作

下面的 PowerShell 代码,Github

中的完整代码

Write-Output "PowerShell Timer trigger function executed at:$(get-date)";

# Get MSI AUTH
$endpoint = $env:MSI_ENDPOINT
$secret = $env:MSI_SECRET
$sqlTokenURI = "https://database.windows.net&api-version=2017-09-01"
$header = @{'Secret' = $secret}
$authenticationResult = Invoke-RestMethod -Method Get -Headers $header -Uri ($endpoint +'?resource=' +$sqlTokenURI)

# CONNECT TO SQL
$SqlServer = $env:SQL_SERVER_NAME
$SqlServerPort = 1433
$Database = "azuredwmonitordb"
$Conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=tcp:$($SqlServer),1433; Initial Catalog=$($Database);")
$Conn.AccessToken = $authenticationResult.access_token

# Open the SQL connection 
$Conn.Open() 

$Cmd=new-object system.Data.SqlClient.SqlCommand("SELECT @@SERVERNAME", $Conn) 
$Cmd.CommandTimeout=120 

# Execute the SQL command 
$Ds=New-Object system.Data.DataSet 
$Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd) 
[void]$Da.fill($Ds) 

# Output the count 
$Ds.Tables.Column1 

# Close the SQL connection 
$Conn.Close()

两个函数实现相同的逻辑:

  1. 从提供者处检索授权令牌
  2. 使用令牌连接到 Azure SQL 服务器

然而,当使用 PowerShell 函数时,第一步有效,但在第二步尝试建立连接时,出现以下错误:

Exception while executing function: Functions.dm_pdw_exec_sessions. Microsoft.Azure.WebJobs.Script: PowerShell script error. System.Management.Automation: Exception calling "Open" with "0" argument(s): "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.". .Net SqlClient Data Provider: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON

我过去曾看到过 Azure SQL 服务器(用户不在主服务器中)未正确启用 AAD 身份验证,但这里不是这种情况。

如果我们想使用 AAD 令牌访问 Azure SQL,我们需要 Provision an Azure Active Directory administrator for your Azure SQL Database server. And create a contained database user representing an application that connects using an Azure AD token

CREATE USER [appName] FROM EXTERNAL PROVIDER;

我的 Azure 帐户不是全局管理员,我发现我无法创建 用户。如果您是全局管理员 azure 帐户,您可以尝试一下。我打算从 microsoft azure 团队那里得到一些帮助,如果有任何回应,我会在这里更新。

您也可以将您的 feedback 交给 Azure 团队。

以下是我的测试步骤。

1.After启用Azure Function,我们可以发现它创建了AD应用程序,但它不在我注册的应用程序下,更多细节请参考截图。

2.Provision Azure SQL 数据库服务器的 Azure Active Directory 管理员

3.Connect 到 Azure Sql 并创建一个代表应用程序的包含数据库用户

Creating [tomtestmsi]... (62,1): SQL72014: .Net SqlClient Data Provider: Msg 33130, Level 16, State 1, Line 1 Principal 'xxxx' could not be found or this principal type is not supported. (62,0): SQL72045: Script execution error. The executed script: CREATE USER [xxxx] FOR EXTERNAL PROVIDER; An error occurred while the batch was being executed.

当前不支持此身份验证方案。

FAQs and known issues with Managed Service Identity (MSI) for Azure Active Directory

MSI 是否与 Active Directory 身份验证库 (ADAL) 或 Microsoft 身份验证库 (MSAL) 一起使用?

否,MSI 尚未与 ADAL 或 MSAL 集成。有关使用 MSI REST 端点获取 MSI 令牌的详细信息,请参阅 How to use an Azure VM Managed Service Identity (MSI) for token acquisition

Web Apps User Voice feedback

问题出在资源 URI 中 - 它缺少正斜杠。而不是:

https://database.windows.net

应该是

https://database.windows.net/

因此将您的 $sqlTokenURI 更改为此,它应该可以工作:

$sqlTokenURI = "https://database.windows.net/&api-version=2017-09-01"