Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 69737bf

Browse files
authored
Merge pull request #26932 from MicrosoftDocs/main
05/01 PM Publishing
2 parents 5517aee + ce58f5d commit 69737bf

18 files changed

Lines changed: 403 additions & 0 deletions
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
---
2+
title: Migrate an application to use passwordless connections with Azure SQL Database
3+
description: Learn how to connect to migrate an application to use passwordless connections with Azure SQL Database
4+
author: alexwolfmsft
5+
ms.author: alexwolf
6+
ms.date: 02/10/2023
7+
ms.service: sql-database
8+
ms.subservice: security
9+
monikerRange: "= azuresql || = azuresql-db"
10+
ms.topic: how-to
11+
ms.custom: devx-track-csharp, passwordless-java, passwordless-js, passwordless-python, passwordless-dotnet, devx-track-azurecli, devx-track-azurepowershell
12+
ms.devlang: csharp
13+
---
14+
15+
# Migrate an application to use passwordless connections with Azure SQL Database
16+
17+
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 application to connect to Azure SQL Database to use passwordless connections instead of a username and password solution.
18+
19+
## Configure the Azure SQL Database
20+
21+
Passwordless connections use Azure Active Directory (Azure AD) authentication to connect to Azure services, including Azure SQL Database. With Azure AD authentication, you can manage identities in a central location to simplify permission management. Learn more about configuring Azure AD authentication for your Azure SQL Database:
22+
23+
- [Azure AD authentication overview](/azure/azure-sql/database/authentication-aad-overview)
24+
- [Configure Azure AD auth](/azure/azure-sql/database/authentication-aad-configure)
25+
26+
For this migration guide, ensure you have an Azure AD admin assigned to your Azure SQL Database.
27+
28+
1) Navigate to the **Azure Active Directory** page of your logical server.
29+
30+
1) Select **Set admin**.
31+
32+
1) In the **Azure Active Directory** flyout menu, search for the user you want to assign as admin.
33+
34+
1) Select the user and choose **Select**.
35+
36+
:::image type="content" source="media/passwordless-connections/migration-enable-active-directory-small.png" lightbox="media/passwordless-connections/migration-enable-active-directory.png" alt-text="A screenshot showing how to enable active directory admin.":::
37+
38+
## Configure your local development environment
39+
40+
Passwordless connections can be configured to work for both local and Azure hosted environments. In this section, you'll apply configurations to allow individual users to authenticate to Azure SQL Database for local development.
41+
42+
### Sign-in to Azure
43+
44+
[!INCLUDE [default-azure-credential-sign-in](../includes/passwordless/default-azure-credential-sign-in.md)]
45+
46+
### Create a database user and assign roles
47+
48+
Create a user in Azure SQL Database. The user should correspond to the Azure account you used to sign-in locally via development tools like Visual Studio or IntelliJ.
49+
50+
1) In the Azure portal, browse to your SQL database and select **Query editor (preview)**.
51+
52+
2) Select **Continue as `<your-username>`** on the right side of the screen to sign into the database using your account.
53+
54+
3) On the query editor view, run the following T-SQL commands:
55+
56+
```sql
57+
CREATE USER <user@domain> FROM EXTERNAL PROVIDER;
58+
ALTER ROLE db_datareader ADD MEMBER <user@domain>;
59+
ALTER ROLE db_datawriter ADD MEMBER <user@domain>;
60+
ALTER ROLE db_ddladmin ADD MEMBER <user@domain>;
61+
GO
62+
```
63+
64+
:::image type="content" source="media/passwordless-connections/query-editor-user-small.png" lightbox="media/passwordless-connections/query-editor-user.png" alt-text="A screenshot showing how to use the Azure Query editor.":::
65+
66+
### Update the local connection configuration
67+
68+
Existing application code that connects to Azure SQL Database using the `Microsoft.Data.SqlClient` library or Entity Framework Core will continue to work with passwordless connections. However, you must update your database connection string to use the passwordless format. For example, the following code works with both SQL authentication and passwordless connections:
69+
70+
```csharp
71+
string connectionString = app.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING")!;
72+
73+
using var conn = new SqlConnection(connectionString);
74+
conn.Open();
75+
76+
var command = new SqlCommand("SELECT * FROM Persons", conn);
77+
using SqlDataReader reader = command.ExecuteReader();
78+
```
79+
80+
To update the referenced connection string (`AZURE_SQL_CONNECTIONSTRING`) to use the passwordless connection string format:
81+
82+
1. Locate your connection string. For local development with .NET applications, this is usually stored in one of the following locations:
83+
* The `appsettings.json` configuration file for your project.
84+
* The `launchsettings.json` configuration file for Visual Studio projects.
85+
* Local system or container environment variables.
86+
87+
2. Replace the connection string value with the following passwordless format. Update the `<database-server-name>` and `<database-name>` placeholders with your own values:
88+
89+
```json
90+
"Server=tcp:<database-server-name>.database.windows.net,1433;Initial Catalog=<database-name>;
91+
Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=\"Active Directory Default\";"
92+
```
93+
94+
### Test the app
95+
96+
Run your app locally and verify that the connections to Azure SQL Database are working as expected. Keep in mind that it may 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.
97+
98+
## Configure the Azure hosting environment
99+
100+
Once your app is configured to use passwordless connections locally, the same code can authenticate to Azure SQL Database after it's deployed to Azure. The sections that follow explain how to configure a deployed application to connect to Azure SQL Database using a [managed identity](/azure/active-directory/managed-identities-azure-resources/overview). Managed identities provide an automatically managed identity in Azure Active Directory (Azure AD) for applications to use when connecting to resources that support Azure AD authentication. Learn more about managed identities:
101+
102+
- [Passwordless overview](/azure/developer/intro/passwordless-overview)
103+
- [Managed identity best practices](/azure/active-directory/managed-identities-azure-resources/managed-identity-best-practice-recommendations)
104+
105+
### Create the managed identity
106+
107+
Create a user-assigned managed identity using the Azure portal or the Azure CLI. Your application uses the identity to authenticate to other services.
108+
109+
# [Azure portal](#tab/azure-portal-create)
110+
111+
1. At the top of the Azure portal, search for *Managed identities*. Select the **Managed Identities** result.
112+
1. Select **+ Create** at the top of the **Managed Identities** overview page.
113+
1. On the **Basics** tab, enter the following values:
114+
* **Subscription**: Select your desired subscription.
115+
* **Resource Group**: Select your desired resource group.
116+
* **Region**: Select a region near your location.
117+
* **Name**: Enter a recognizable name for your identity, such as *MigrationIdentity*.
118+
1. Select **Review + create** at the bottom of the page.
119+
1. When the validation checks finish, select **Create**. Azure creates a new user-assigned identity.
120+
121+
After the resource is created, select **Go to resource** to view the details of the managed identity.
122+
123+
:::image type="content" source="media/passwordless-connections/create-managed-identity-portal-small.png" lightbox="media/passwordless-connections/create-managed-identity-portal.png" alt-text="A screenshot showing how to create a managed identity using the Azure portal.":::
124+
125+
# [Azure CLI](#tab/azure-cli-create)
126+
127+
Use the [az identity create](/cli/azure/identity) command to create a user-assigned managed identity:
128+
129+
```azurecli
130+
az identity create --name MigrationIdentity --resource-group <resource-group>
131+
```
132+
133+
---
134+
135+
## Associate the managed identity with your web app
136+
137+
Configure your web app to use the user-assigned managed identity you created.
138+
139+
# [Azure portal](#tab/azure-portal-assign)
140+
141+
Complete the following steps in the Azure portal to associate the user-assigned managed identity with your app. These same steps apply to the following Azure services:
142+
143+
* Azure Spring Apps
144+
* Azure Container Apps
145+
* Azure virtual machines
146+
* Azure Kubernetes Service
147+
* Navigate to the overview page of your web app.
148+
149+
1) Select **Identity** from the left navigation.
150+
151+
1) On the **Identity** page, switch to the **User assigned** tab.
152+
153+
1) Select **+ Add** to open the **Add user assigned managed identity** flyout.
154+
155+
1) Select the subscription you used previously to create the identity.
156+
157+
1) Search for the **MigrationIdentity** by name and select it from the search results.
158+
159+
1) Select **Add** to associate the identity with your app.
160+
161+
:::image type="content" source="media/passwordless-connections/assign-managed-identity-small.png" lightbox="media/passwordless-connections/assign-managed-identity.png" alt-text="A screenshot showing how to assign a managed identity.":::
162+
163+
# [Azure CLI](#tab/azure-cli-assign)
164+
165+
[!INCLUDE [associate-managed-identity-cli](../includes/passwordless/associate-managed-identity-cli.md)]
166+
167+
# [Service Connector](#tab/service-connector-assign)
168+
169+
[!INCLUDE [service-connector-commands](../includes/passwordless/service-connector-commands.md)]
170+
171+
---
172+
173+
### Create a database user for the identity and assign roles
174+
175+
Create a SQL database user that maps back to the user-assigned managed identity. Assign the necessary SQL roles to the user to allow your app to read, write, and modify the data and schema of your database.
176+
177+
1) In the Azure portal, browse to your SQL database and select **Query editor (preview)**.
178+
179+
2) Select **Continue as `<username>`** on the right side of the screen to sign into the database using your account.
180+
181+
3) On the query editor view, run the following T-SQL commands:
182+
183+
```sql
184+
CREATE USER <user-assigned-identity-name> FROM EXTERNAL PROVIDER;
185+
ALTER ROLE db_datareader ADD MEMBER <user-assigned-identity-name>;
186+
ALTER ROLE db_datawriter ADD MEMBER <user-assigned-identity-name>;
187+
ALTER ROLE db_ddladmin ADD MEMBER <user-assigned-identity-name>;
188+
GO
189+
```
190+
191+
:::image type="content" source="media/passwordless-connections/query-editor-identity-small.png" lightbox="media/passwordless-connections/query-editor-identity.png" alt-text="A screenshot showing how to use the Azure Query editor to create a SQL user for a managed identity.":::
192+
193+
---
194+
195+
> [!IMPORTANT]
196+
> Use caution when assigning database user roles in enterprise production environments. In those scenarios, the app shouldn't perform all operations using a single, elevated identity. Try to implement the principle of least privilege by configuring multiple identities with specific permissions for specific tasks.
197+
>
198+
> You can read more about configuring database roles and security on the following resources:
199+
>
200+
> * [Tutorial: Secure a database in Azure SQL Database](/azure/azure-sql/database/secure-database-tutorial)
201+
> * [Authorize database access to SQL Database](/azure/azure-sql/database/logins-create-manage)
202+
203+
### Update the connection string
204+
205+
Update your Azure app configuration to use the passwordless connection string format. Connection strings are generally stored as environment variables in your app hosting environment. The following instructions focus on App Service, but other Azure hosting services provide similar configurations.
206+
207+
1. Navigate to the configuration page of your App Service instance and locate the Azure SQL Database connection string.
208+
209+
1. Select the edit icon and update the connection string value to match following format. Change the `<database-server-name>` and `<database-name>` placeholders with the values of your own service.
210+
211+
```json
212+
"Server=tcp:<database-server-name>.database.windows.net,1433;Initial Catalog=<database-name>;
213+
Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=\"Active Directory Default\";"
214+
```
215+
216+
1. Save your changes and restart the application if it does not do so automatically.
217+
218+
### Test the application
219+
220+
Test your app to make sure everything is still working. It may take a few minutes for all of the changes to propagate through your Azure environment.
221+
222+
## Next steps
223+
224+
In this tutorial, you learned how to migrate an application to passwordless connections.
225+
226+
You can read the following resources to explore the concepts discussed in this article in more depth:
227+
228+
- [Passwordless overview](/azure/developer/intro/passwordless-overview)
229+
- [Managed identity best practices](/azure/active-directory/managed-identities-azure-resources/managed-identity-best-practice-recommendations)
230+
- [Tutorial: Secure a database in Azure SQL Database](/azure/azure-sql/database/secure-database-tutorial)
231+
- [Authorize database access to SQL Database](/azure/azure-sql/database/logins-create-manage)
69.2 KB
Loading
83.6 KB
Loading
-2.02 KB
Loading
-998 Bytes
Loading
61.9 KB
Loading
35.6 KB
Loading
74.6 KB
Loading
60.7 KB
Loading
61.6 KB
Loading

0 commit comments

Comments
 (0)