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
Copy file name to clipboardExpand all lines: azure-sql/database/azure-sql-passwordless-migration-python.md
+34-27Lines changed: 34 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,8 @@ title: Migrate a Python Application to Use Passwordless Connections
3
3
description: Learn how to migrate a Python application to use passwordless connections with Azure SQL Database.
4
4
author: WilliamDAssafMSFT
5
5
ms.author: wiassaf
6
-
ms.reviewer: rotabor, mathoma
7
-
ms.date: 06/13/2025
6
+
ms.reviewer: dlevy, rotabor, mathoma
7
+
ms.date: 01/29/2026
8
8
ms.service: azure-sql-database
9
9
ms.subservice: security
10
10
ms.topic: how-to
@@ -23,6 +23,8 @@ ms.custom:
23
23
24
24
Application requests to Azure SQL Database must be authenticated. Although there are multiple options for authenticating to Azure SQL Database, you should prioritize passwordless connections in your applications when possible. Traditional authentication methods that use passwords or secret keys create security risks and complications. Visit the [passwordless connections for Azure services](/azure/developer/intro/passwordless-overview) hub to learn more about the advantages of moving to passwordless connections. The following tutorial explains how to migrate an existing Python application to connect to Azure SQL Database to use passwordless connections instead of a username and password solution.
25
25
26
+
The [mssql-python](/sql/connect/python/mssql-python/python-sql-driver-mssql-python) driver provides built-in support for Microsoft Entra authentication, making passwordless connections straightforward with minimal code changes.
@@ -43,40 +45,39 @@ Create a user in Azure SQL Database. The user should correspond to the Azure acc
43
45
44
46
### Update the local connection configuration
45
47
46
-
Existing application code that connects to Azure SQL Database using the [Python SQL Driver - pyodbc](/sql/connect/python/pyodbc/python-sql-driver-pyodbc) continues to work with passwordless connections with minor changes. For example, the following code works with both SQL authentication and passwordless connections when running locally and when deployed to Azure App Service.
48
+
Migrating to passwordless connections with [mssql-python](/sql/connect/python/mssql-python/python-sql-driver-mssql-python) requires only a connection string change. The driver has built-in support for Microsoft Entra authentication modes, eliminating the need for manual token handling.
> In this example code, the App Service environment variable `WEBSITE_HOSTNAME` is used to determine what environment the code is running in. For other deployment scenarios, you can use other environment variables to determine the environment.
67
+
To update the referenced connection string (`AZURE_SQL_CONNECTIONSTRING`) for local development, create a `.env` file in your project folder with the passwordless connection string format using `ActiveDirectoryDefault` authentication:
73
68
74
-
To update the referenced connection string (`AZURE_SQL_CONNECTIONSTRING`) for local development, use the passwordless connection string format:
75
-
76
-
```
77
-
Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30
`ActiveDirectoryDefault` automatically discovers credentials from multiple sources (Azure CLI, environment variables, Visual Studio, etc.) without requiring interactive login. This approach is convenient for development but adds latency because it tries each credential source in sequence.
74
+
75
+
> [!IMPORTANT]
76
+
> `ActiveDirectoryDefault` is intended for local development only. It tries multiple authentication methods in sequence, which adds latency and can cause unexpected behavior in production. For production applications, use the specific authentication method for your scenario:
77
+
> -**Azure App Service/Functions**: Use `ActiveDirectoryMSI` (managed identity)
78
+
> -**Interactive user login**: Use `ActiveDirectoryInteractive`
79
+
> -**Service principal**: Use `ActiveDirectoryServicePrincipal`
80
+
80
81
### Test the app
81
82
82
83
Run your app locally and verify that the connections to Azure SQL Database are working as expected. Keep in mind that it can take several minutes for changes to Azure users and roles to propagate through your Azure environment. Your application is now configured to run locally without developers having to manage secrets in the application itself.
@@ -104,7 +105,8 @@ Complete the following steps in the Azure portal to associate the user-assigned
104
105
- Azure Container Apps
105
106
- Azure virtual machines
106
107
- Azure Kubernetes Service
107
-
- Navigate to the overview page of your web app.
108
+
109
+
1. Navigate to the overview page of your web app.
108
110
109
111
1. Select **Identity** from the left navigation.
110
112
@@ -132,12 +134,12 @@ Complete the following steps in the Azure portal to associate the user-assigned
132
134
133
135
### Update the connection string
134
136
135
-
Update your Azure app configuration to use the passwordless connection string format. The format should be the same used in your local environment.
137
+
Update your Azure app configuration to use the passwordless connection string format with `ActiveDirectoryMSI` authentication for managed identity.
136
138
137
139
Connection strings can be stored as environment variables in your app hosting environment. The following instructions focus on App Service, but other Azure hosting services provide similar configurations.
138
140
139
141
```connectionstring
140
-
Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30
`<database-server-name>` is the name of your Azure SQL Database server and `<database-name>` is the name of your Azure SQL Database.
@@ -149,16 +151,21 @@ To use the user-assigned managed identity, create an `AZURE_CLIENT_ID` environme
149
151
Save your changes and restart the application if it doesn't do so automatically.
150
152
151
153
> [!NOTE]
152
-
> The example connection code shown in this migration guide uses the [DefaultAzureCredential](/python/api/azure-identity/azure.identity.defaultazurecredential) class when deployed. Specifically, it uses the DefaultAzureCredential without passing the user-assigned managed identity client ID to the constructor. In this scenario, the fallback is to check for the AZURE_CLIENT_ID environment variable. If the AZURE_CLIENT_ID environment variable doesn't exist, a system-assigned managed identity will be used if configured.
154
+
> When using a user-assigned managed identity, include the client ID in the connection string using the `User Id` parameter:
> If you pass the managed identity client ID in the DefaultAzureCredential constructor, the connection code can still be used locally and deployed because the authentication process falls back to interactive authentication in the local scenario. For more information, see the [Azure Identity client library for Python](/python/api/overview/azure/identity-readme#defaultazurecredential).
160
+
> If you omit the `User Id` parameter, the driver uses the system-assigned managed identity if one is configured.
155
161
156
162
### Test the application
157
163
158
164
Test your app to make sure everything is still working. It can take a few minutes for all of the changes to propagate through your Azure environment.
0 commit comments