gpt4 book ai didi

azure - 使用 MFA Active Directory 交互式身份验证在 Python 中连接到 Azure SQL,而不使用 Microsoft.IdentityModel.Clients.ActiveDirectory dll

转载 作者:行者123 更新时间:2023-12-02 22:49:19 25 4
gpt4 key购买 nike

使用 MFA(在 SSMS 中作为“Active Directory - 通用”)连接到 Azure SQL 数据库 Microsoft 建议,目前只有有关使用 Microsoft.IdentityModel.Clients.ActiveDirectory 与 C# 连接的教程

在 Python 或 Powershell 的常规 ODBC 连接字符串中设置 Authentication='Active Directory Interactive'; 会导致错误

Cannot find an authentication provider for 'ActiveDirectoryInteractive'

这似乎是因为根据 Microsoft 的示例代码 https://learn.microsoft.com/en-us/azure/sql-database/active-directory-interactive-connect-azure-sql-db创建连接时,您需要显式创建自己的身份验证提供程序类:


public static void Main(string[] args)
{
var provider = new ActiveDirectoryAuthProvider();

SC.SqlAuthenticationProvider.SetProvider(
SC.SqlAuthenticationMethod.ActiveDirectoryInteractive,
//SC.SqlAuthenticationMethod.ActiveDirectoryIntegrated, // Alternatives.
//SC.SqlAuthenticationMethod.ActiveDirectoryPassword,
provider);

Program.Connection();
}

我想与 pyodbc 连接,因此无法实现 ActiveDirectoryInteractive 提供程序。

是否有任何方法可以使用 OAuth 一般获取 token 并在连接字符串中使用它,或者以其他方式实现 ActiveDirectoryInteractive 提供程序而不使用 .NET?

最佳答案

在 MacOS/Linux 中使用 AAD MFA 进行身份验证

我在 MacO 上遇到了同样的问题。如上所述,使用“ActiveDirectoryInteractive”的 ODBC 选项仅适用于 Windows。

如果您想查看这篇文章的更详细版本,请查看我在 Medium 上的帖子,否则继续阅读。 ;)

要求

为了使用 AAD MFA 连接到数据库,我还使用了 pyodbc 但带有访问 token 。要获取 token ,您需要执行以下操作:

  1. Azure CLI

  2. Microsoft ODBC Driver for SQL Server (Linux-MAC)

说明

在运行下面的代码之前,必须使用 azure cli 进行身份验证,然后从 cmd 运行:az login

from azure.identity import AzureCliCredential
import struct
import pyodbc

# input params
server = '<your server address>'
database = '<database name>'
query = 'SELECT * from dbo.Address;'

# Use the cli credential to get a token after the user has signed in via the Azure CLI 'az login' command.
credential = AzureCliCredential()
databaseToken = credential.get_token('https://database.windows.net/')

# get bytes from token obtained
tokenb = bytes(databaseToken[0], "UTF-8")
exptoken = b'';
for i in tokenb:
exptoken += bytes({i});
exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;

# build connection string using acquired token
connString = "Driver={ODBC Driver 17 for SQL Server};SERVER="+server+";DATABASE="+database+""
SQL_COPT_SS_ACCESS_TOKEN = 1256
conn = pyodbc.connect(connString, attrs_before = {SQL_COPT_SS_ACCESS_TOKEN:tokenstruct});

# sample query
cursor = conn.cursor()
cursor.execute(query)
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()

疑难解答

根据 ODBC 驱动程序和 MacOS 的版本,有些人使用上述代码时可能会遇到不同的行为。

  1. 确保始终使用最新版本的 ODBC 驱动程序
  2. 如果您的连接返回类似于“SSL 提供商:[错误:0A000086:SSL 例程::证书验证失败:无法获取本地颁发者证书] (-1)”的错误。将 TrustServerCertificate=Yes; 添加到连接字符串会有所帮助。

引用文献

https://pypi.org/project/azure-identity/

https://github.com/AzureAD/azure-activedirectory-library-for-python/wiki/Connect-to-Azure-SQL-Database

关于azure - 使用 MFA Active Directory 交互式身份验证在 Python 中连接到 Azure SQL,而不使用 Microsoft.IdentityModel.Clients.ActiveDirectory dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com