You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')# The value should be 'ActiveDirectoryMsi'
89
89
90
90
# Uncomment the following lines according to the authentication type.
91
91
# For system-assigned managed identity.
@@ -97,7 +97,7 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
97
97
98
98
conn = pyodbc.connect(connString)
99
99
```
100
-
Refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python) to connect to Azure SQL Database using an access token.
100
+
For an alternative method, you can also connect to Azure SQL Database using an access token, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python).
101
101
102
102
### [NodeJS](#tab/nodejs)
103
103
@@ -135,13 +135,11 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
Copy file name to clipboardExpand all lines: articles/app-service/tutorial-connect-msi-azure-database.md
+14-10Lines changed: 14 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,26 +188,30 @@ If you encounter any problem when creating a connection, refer to [Troubleshooti
188
188
189
189
## 3. Modify your code
190
190
191
-
In this section, connectivity to the Azure database in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
192
-
193
-
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
194
-
2. Get an access token for the resource URI respective to the database type.
195
-
* For Azure SQL Database: `https://database.windows.net/.default`
196
-
* For Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`
197
-
* For Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`
198
-
3. Add the token to your connection string.
199
-
4. Open the connection.
200
-
201
191
# [Azure SQL Database](#tab/sqldatabase-sc)
202
192
203
193
[!INCLUDE [code sample for sql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-sql-mi.md)]
204
194
205
195
# [Azure Database for MySQL](#tab/mysql-sc)
206
196
197
+
Connectivity to the Azure Database for MySQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
198
+
199
+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
200
+
2. Get an access token for Azure Database for MySQL: `https://ossrdbms-aad.database.windows.net/.default`.
201
+
3. Add the token to your connection string.
202
+
4. Open the connection.
203
+
207
204
[!INCLUDE [code sample for mysql managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-mysql-mi.md)]
208
205
209
206
# [Azure Database for PostgreSQL](#tab/postgresql-sc)
210
207
208
+
Connectivity to the Azure Database for PostgreSQL in your code follows the `DefaultAzureCredential` pattern for all language stacks. `DefaultAzureCredential` is flexible enough to adapt to both the development environment and the Azure environment. When running locally, it can retrieve the logged-in Azure user from the environment of your choice (Visual Studio, Visual Studio Code, Azure CLI, or Azure PowerShell). When running in Azure, it retrieves the managed identity. So it's possible to have connectivity to database both at development time and in production. The pattern is as follows:
209
+
210
+
1. Instantiate a `DefaultAzureCredential` from the Azure Identity client library. If you're using a user-assigned identity, specify the client ID of the identity.
211
+
2. Get an access token for Azure Database for PostgreSQL: `https://ossrdbms-aad.database.windows.net/.default`.
212
+
3. Add the token to your connection string.
213
+
4. Open the connection.
214
+
211
215
[!INCLUDE [code sample for postgres managed identity authentication connection](./includes/tutorial-connect-msi-azure-database/code-postgres-mi.md)]
0 commit comments