简体   繁体   English

pyodbc 是否支持使用 AD 访问令牌而不是用户/密码连接到 Azure SQL DB?

[英]pyodbc will support connecting to an Azure SQL DB using the AD access token instead of user/password?

Currently, I use device code credential to get the access to Azure AD.目前,我使用设备代码凭证来访问 Azure AD。

device_code_credential = DeviceCodeCredential(
        azure_client_id,
        tenant_id=azure_tenant_id,
        authority=azure_authority_uri)

But I still need to use Azure account username/password to connect to Azure SQL server但我仍然需要使用 Azure 帐户用户名/密码连接到 Azure SQL 服务器

driver = 'ODBC Driver 17 for SQL Server'
db_connection_string = f'DRIVER={driver};SERVER={server};' \
    f'DATABASE={database};UID={user_name};PWD={password};'\
    f'Authentication=ActiveDirectoryPassword;'\
    'Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'
connector = pyodbc.connect(db_connection_string)

Is any way in python under linux/MacOS can allow me to use device_code_credential and access_token to connect to Azure SQL server?在 linux/MacOS 下的 python 中是否有任何方法可以让我使用 device_code_credential 和 access_token 连接到 Azure SQL 服务器?

https://github.com/mkleehammer/pyodbc/issues/228 https://github.com/mkleehammer/pyodbc/issues/228

I only got this link and it doesn't seem to work.我只有这个链接,它似乎不起作用。

Anyone has a fully working sample?有人有完整的工作样本吗?

You could reference this tutorial: AzureAD/azure-activedirectory-library-for-python: Connect to Azure SQL Database .您可以参考本教程: AzureAD/azure-activedirectory-library-for-python: Connect to Azure SQL 数据库

It is doable to connect to Azure SQL Database by obtaining a token from Azure Active Directory (AAD), via ADAL Python. It is doable to connect to Azure SQL Database by obtaining a token from Azure Active Directory (AAD), via ADAL Python. We do not currently maintain a full sample for it, but this essay outlines some key ingredients.我们目前没有为其保留完整的样本,但本文概述了一些关键要素。

  1. You follow the instruction of Connecting using Access Token to provision your application.您按照使用访问令牌进行连接的说明来配置您的应用程序。 There is another similar blog post here .这里还有另一篇类似的博客文章
  2. Your SQL admin need to add permissions for the app-registration to the specific database that you are trying to access.您的 SQL 管理员需要将应用注册权限添加到您尝试访问的特定数据库。 See details in this blog post Token-based authentication support for Azure SQL DB using Azure AD auth by Mirek H Sztajno.请参阅此博客文章中基于令牌的身份验证支持 Azure SQL DB 的详细信息,使用 Azure AD auth by Mirek H Szt
  3. It was not particularly highlighted in either of the documents above, but you need to use https://database.windows.net/ as the resource string.上面两个文档中都没有特别强调,但是您需要使用https://database.windows.net/作为资源字符串。 Note that you need to keep the trailing slash, otherwise the token issued would not work.请注意,您需要保留尾部斜杠,否则发出的令牌将不起作用。
  4. Feed the configuration above into ADAL Python's Client Credentials sample .将上述配置输入 ADAL Python 的客户端凭据示例
  5. Once you get the access token, use it in this way in pyodbc to connect to SQL Database.获得访问令牌后, 在 pyodbc 中以这种方式使用它来连接到 SQL 数据库。

This works with AAD access tokens.这适用于 AAD 访问令牌。 Example code to expand the token and prepend the length as described on the page linked above, in Python 2.x:在 Python 2.x 中,如上面链接的页面所述,扩展令牌并添加长度的示例代码:

token = "eyJ0eXAiOi...";
exptoken = "";
for i in token:
    exptoken += i;
    exptoken += chr(0);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
conn = pyodbc.connect(connstr, attrs_before = { 1256:bytearray(tokenstruct) });

3.x is only slightly more involved due to annoying char/bytes split:由于烦人的字符/字节拆分,3.x 只涉及稍微多一点:

token = b"eyJ0eXAiOi...";
exptoken = b"";
for i in token:
    exptoken += bytes({i});
    exptoken += bytes(1);
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
conn = pyodbc.connect(connstr, attrs_before = { 1256:tokenstruct });

(SQL_COPT_SS_ACCESS_TOKEN is 1256; it's specific to msodbcsql driver so pyodbc does not have it defined, and likely will not.) (SQL_COPT_SS_ACCESS_TOKEN 是 1256;它特定于 msodbcsql 驱动程序,因此 pyodbc 没有定义它,并且可能不会。)

Hope this helps.希望这可以帮助。

You can get a token via您可以通过

from azure.identity import DeviceCodeCredential

# Recommended to allocate a new ClientID in your tenant.
AZURE_CLI_CLIENT_ID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
credential = DeviceCodeCredential(client_id=AZURE_CLI_CLIENT_ID)
databaseToken = credential.get_token('https://database.windows.net/.default')

Then use databaseToken.token as an AAD Access Token as described in Leon Yue's answer.然后使用 databaseToken.token 作为 Leon Yue 的回答中描述的 AAD 访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于令牌的身份验证支持 Azure SQL DB 使用 Azure AD 身份验证 - Token-based authentication support for Azure SQL DB using Azure AD auth 使用来自ASP.NET应用程序的AD密码身份验证连接到Azure SQL失败,并显示“需要用户界面才能继续” - Connecting to Azure SQL with AD Password Authentication from ASP.NET app fails with “Need user interface to continue” Azure SQL 使用 PowerShell 和 ServicePrincipal 为 AD 用户授予访问权限 - Azure SQL Grant Access for AD User using PowerShell and ServicePrincipal 如何使用Powershell为用户获取Azure SQL DB密码 - How to get Azure SQL DB Password for user using powershell 提取 Azure SQL DB 中的散列用户密码 - Extract hashed user password in Azure SQL DB 使用 AD 令牌访问在 Azure AD 中注册的多个应用程序 - Using AD Token for access to multiple applications registered in Azure AD 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时出现问题 - Trouble connecting to Sql Azure Db from C# using Active Directory password 使用Azure AD帐户将Azure Logic应用连接到SQL - Connecting Azure Logic App to SQL using Azure AD Account 如何使用AzureADGraph或Microsoft Graph在AZURE AD B2C中为用户生成访问令牌? - How to generate access token for user in AZURE AD B2C using AzureADGraph or Microsoft Graph? 如何获取已登录用户的Azure AD访问令牌? - How to get Azure AD access token for already logged in user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM