python - 当身份验证类型为 Azure Active Directory 时,通过 Python 连接到 sql-server

标签 python sql-server azure-active-directory

我正在尝试查看当身份验证类型为:时是否可以通过 Python 连接到 SQL Server:

Azure Active Directory - Universal with MFA support

我可以使用 Azure 连接到此数据库。下面的行显示了通过 azure data studio 连接时所需的凭据:

connection type : Microsoft SQL Server
server : ***.net
Authentication Type : Azure Active Directory - Universal with MFA support
account : ***.***@mycompany.com
database : target
Server group : default

我在网络上到处查看,我看到的有关通过 Python 连接到 SQL Server 的所有内容都是当身份验证类型为 SQL 登录时。如果您知道答案请帮忙。

最佳答案

是的,可以。引用this document了解更多详情。

(Windows driver only.) AAD Interactive Authentication uses Azure Multi-factor Authentication technology to set up connection. In this mode, by providing the login ID, an Azure Authentication dialog is triggered and allows the user to input the password to complete the connection. The username is passed in the connection string.

server=Server;database=Database;UID=UserName;Authentication=ActiveDirectoryInteractive;

我已经对此进行了测试,我的测试代码如下。

import pyodbc
server = 'tonytest920.database.windows.net'
database = 'tonytest'
driver = '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server +
                      ';PORT=1433;DATABASE='+database+';Authentication=ActiveDirectoryInteractive;UID=xx@xx.onmicrosoft.com')
cursor = cnxn.cursor()
cursor.execute(
    "select * from Persons")
row = cursor.fetchone()
while row:
    print(str(row[0]) + " " + str(row[1]))
    row = cursor.fetchone()

注意:您需要安装adalsql.dll第一的。请记住选择ENU\x86\adalsql.msi

关于python - 当身份验证类型为 Azure Active Directory 时,通过 Python 连接到 sql-server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58014102/

相关文章:

SQL舍入值

mysql - 我可以从 DTS 迁移到等效的 MySQL 工具吗?

c# - ExecuteNonQuery 不更新输出参数

python - arr[范围(3)]与arr[:3] in numpy的区别

python - 什么是 "TPE1"KeyError?

azure - Authorization_IdentityNotFound 访问图形 API 时出错

c# - 如果服务主体被授予其所属的组,我如何验证该角色是否具有应用程序角色?

python - Azure AD 身份验证 Python Web API

python - 如何通过weather api获取降水量/降雨量?

根据第二列的值计算表达式(方程式)的 Python 程序