<small id='riqhp'></small><noframes id='riqhp'>

    • <bdo id='riqhp'></bdo><ul id='riqhp'></ul>
    1. <tfoot id='riqhp'></tfoot>

      <legend id='riqhp'><style id='riqhp'><dir id='riqhp'><q id='riqhp'></q></dir></style></legend>
    2. <i id='riqhp'><tr id='riqhp'><dt id='riqhp'><q id='riqhp'><span id='riqhp'><b id='riqhp'><form id='riqhp'><ins id='riqhp'></ins><ul id='riqhp'></ul><sub id='riqhp'></sub></form><legend id='riqhp'></legend><bdo id='riqhp'><pre id='riqhp'><center id='riqhp'></center></pre></bdo></b><th id='riqhp'></th></span></q></dt></tr></i><div id='riqhp'><tfoot id='riqhp'></tfoot><dl id='riqhp'><fieldset id='riqhp'></fieldset></dl></div>

      1. 使用托管标识向SQL数据库验证Azure应用程序服务

        Use Managed Identity to authenticate Azure App Service to SQL Database(使用托管标识向SQL数据库验证Azure应用程序服务)
          <tbody id='wuVSO'></tbody>
      2. <i id='wuVSO'><tr id='wuVSO'><dt id='wuVSO'><q id='wuVSO'><span id='wuVSO'><b id='wuVSO'><form id='wuVSO'><ins id='wuVSO'></ins><ul id='wuVSO'></ul><sub id='wuVSO'></sub></form><legend id='wuVSO'></legend><bdo id='wuVSO'><pre id='wuVSO'><center id='wuVSO'></center></pre></bdo></b><th id='wuVSO'></th></span></q></dt></tr></i><div id='wuVSO'><tfoot id='wuVSO'></tfoot><dl id='wuVSO'><fieldset id='wuVSO'></fieldset></dl></div>
        • <bdo id='wuVSO'></bdo><ul id='wuVSO'></ul>
          <legend id='wuVSO'><style id='wuVSO'><dir id='wuVSO'><q id='wuVSO'></q></dir></style></legend>

            1. <small id='wuVSO'></small><noframes id='wuVSO'>

                <tfoot id='wuVSO'></tfoot>
                  本文介绍了使用托管标识向SQL数据库验证Azure应用程序服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将在Azure应用服务Web App中运行的Python Flask应用连接到Azure SQL数据库。 当我将SQL身份验证与用户名和密码一起使用时,工作正常。

                  现在,我想转到使用Web应用程序托管标识。 我已经激活了系统分配的托管标识,在SQL中为其创建了一个用户,并将其添加到db_datareader角色。

                  我正在使用如下所示的连接字符串连接SqlAlChemy

                  params = urllib.parse.quote_plus(os.environ['SQL_CONNECTION_STRING'])
                  conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
                  engine_azure = db.create_engine(conn_str,echo=True)
                  

                  连接字符串存储为应用程序设置,其值为

                  "Driver={ODBC Driver 17 for SQL Server};Server=tcp:<server>.database.windows.net,1433;Database=<database>;Authentication=ActiveDirectoryMsi;"
                  

                  我以为这就是我需要做的全部工作,但现在我的应用程序无法启动。 日志在连接到数据库时报告超时。

                  如何修复此问题?

                  推荐答案

                  如果您想在Python应用中连接Azure SQL数据库和Azure MSI,我们可以使用SDKpyodbc来实现。

                  例如

                  1. Enable system-assigned identity for your Azure app service

                  2. Add the MSi as contained database users in your database

                    a.将您的SQL数据库与Azure SQL AD管理员连接(我使用SSMS来实现)

                    B.在数据库中运行以下脚本

                    CREATE USER <your app service name> FROM EXTERNAL PROVIDER;
                    ALTER ROLE db_datareader ADD MEMBER <your app service name>
                    ALTER ROLE db_datawriter ADD MEMBER <your app service name>
                    ALTER ROLE db_ddladmin ADD MEMBER <your app service name>
                    
                  3. 编码
                  import os
                  import pyodbc
                  import requests 
                  import struct
                  
                  #get access token
                  identity_endpoint = os.environ["IDENTITY_ENDPOINT"]
                  identity_header = os.environ["IDENTITY_HEADER"]
                  resource_uri="https://database.windows.net/"
                  token_auth_uri = f"{identity_endpoint}?resource={resource_uri}&api-version=2019-08-01"
                  head_msi = {'X-IDENTITY-HEADER':identity_header}
                  resp = requests.get(token_auth_uri, headers=head_msi)
                  access_token = resp.json()['access_token']
                  
                  
                  accessToken = bytes(access_token, 'utf-8');
                  exptoken = b"";
                  for i in accessToken:
                          exptoken += bytes({i});
                          exptoken += bytes(1);
                  tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
                  
                  conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=tcp:andyserver.database.windows.net,1433;Database=database2", attrs_before = { 1256:bytearray(tokenstruct) });
                  
                  cursor = conn.cursor()
                  cursor.execute("select @@version")
                  row = cursor.fetchall()
                  

                  有关更多详细信息,请参阅

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

                  https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity

                  https://docs.microsoft.com/en-us/azure/sql-database/sql-database-aad-authentication-configure

                  这篇关于使用托管标识向SQL数据库验证Azure应用程序服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                  <legend id='WQA7h'><style id='WQA7h'><dir id='WQA7h'><q id='WQA7h'></q></dir></style></legend>
                    <i id='WQA7h'><tr id='WQA7h'><dt id='WQA7h'><q id='WQA7h'><span id='WQA7h'><b id='WQA7h'><form id='WQA7h'><ins id='WQA7h'></ins><ul id='WQA7h'></ul><sub id='WQA7h'></sub></form><legend id='WQA7h'></legend><bdo id='WQA7h'><pre id='WQA7h'><center id='WQA7h'></center></pre></bdo></b><th id='WQA7h'></th></span></q></dt></tr></i><div id='WQA7h'><tfoot id='WQA7h'></tfoot><dl id='WQA7h'><fieldset id='WQA7h'></fieldset></dl></div>
                      <tbody id='WQA7h'></tbody>

                      <tfoot id='WQA7h'></tfoot>
                        <bdo id='WQA7h'></bdo><ul id='WQA7h'></ul>

                          <small id='WQA7h'></small><noframes id='WQA7h'>